目录
对象嵌套关联查询一对多List集合查询
mybatis嵌套关联查询如下
一对多查询(经典案例)
条件
数据库
代码实现

对象嵌套关联查询一对多List集合查询

mybatis嵌套关联查询如下
由于我的是一对集合查询,所以我有两个类。
@Data@TableName("tb_user")public class User { @TableId(type= IdType.INPUT) private String id; @TableField("user_name") private String username; private String password; private String name; private String email; private int age; private ArrayList<Authority> list;}
权限类
@Data@TableNamepublic class Authority { @TableId(type = IdType.INPUT) @TableField("aid") private int id; @TableId("aname") private String name;}
测试类
@Test public void ManyToMany(){ User user = userMapper.selectAuthorityById(1); ArrayList <Authority> list = user.getList(); System.out.println(user); for (Authority authority : list) { System.out.println("所对应权限为"+authority.getName()); } }
springboot项目的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis plus 起步依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency>
这下面就是我xml文件里面怎么写的嵌套查询语句
<mapper namespace="com.itheima.mybatisplus.mapper.UserMapper"> <!--返回的对象为authority--> <resultMap id="authority" type="com.itheima.mybatisplus.domain.User"> <id column="id" property="id"/> <id column="password" property="password"/> <id column="age" property="age"/> <id column="email" property="email"/> <id column="name" property="name"/> <id column="user_name" property="username"/> <collection property="list" ofType="com.itheima.mybatisplus.domain.Authority"> <id property="id" column="aid"/> <id property="name" column="aname"/> </collection> <select id="selectAuthorityById" parameterType="int" resultMap="authority"> SELECT * FROM authority a,tb_user t,user_authority ua WHERE a.aid=ua.authority_id AND t.id=ua.user_id AND t.id=#{id} </select>
数据库的配置我就不放了,直接编写就可以了,看会下面这个xml配置就可以了

一对多查询(经典案例)

条件
查询班级表 返回所有学生信息 (一对多问题)

数据库
Java技术迷

班级class_info

学生student

代码实现
<!-- 多对一 或者 一对一 --><!-- <association property=""--><!-- 一对多 返回集合--><!- - <collection property=""- ->
实体类ClassInfo.java
`@Data
public class ClassInfo {

  1. private Long id;
  2. private String name;
  3. private String nameTest;
  4. private List<Student> studentList;

}ClassInfoMapper.xml<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “">http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--名称空间:对应mapper层某个接口的包的全名称-->

<mapper namespace="com.example.demo.mapper.ClassInfoMapper">

  1. <!-- 查询班级 返回所有学生的信息 一对多-->
  2. <!-- 自定义映射规则-->
  3. <resultMap id="OneToMany" type="com.qcby.zsgc.entity.ClassInfo">
  4. <result column="name" jdbcType="VARCHAR" property="nameTest" />
  5. <collection column="{id1=id,name=name}"
  6. property="studentList"
  7. select="com.example.demo.mapper.StudentMapper.listByClassInfoId"> </collection>
  8. </resultMap>
  9. <select id="listAllWithStudent" resultMap="OneToMany">
  10. select * from class_info

关联StudentMapper.xml中的子查询
<select id="listByClassInfoId" resultType="com.example.demo.entity.Student"> SELECT * FROM student s where class_info_id = #{id1} or name = #{name} </select>
ClassInfoMapper.java
public interface ClassInfoMapper extends BaseMapper<ClassInfo> { IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page); }
ClassInfoService.java
public interface ClassInfoService extends IService<ClassInfo> { IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page); }
ClassInfoServiceImpl.java
@Servicepublic class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements ClassInfoService { @Autowired private StudentService studentService; @Override public IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page) { return this.baseMapper.listAllWithStudent(page); }}
ClassInfoController.java
`@Controller
@RequestMapping(“classInfo”)
public class ClassInfoController {

  1. @Autowired
  2. private ClassInfoService classInfoService;
  3. @RequestMapping("listAllWithStudent")
  4. @ResponseBody
  5. public IPage<ClassInfo> listAllWithStudent(Integer pageNo,Integer pageSize){
  6. Page<ClassInfo> page = new Page<>(pageNo,pageSize);
  7. return classInfoService.listAllWithStudent(page);
  8. }

}`

更多相关文章

  1. Android自带的TabLayout实现滑动翻页效果(实例)
  2. Android中ScrollView嵌套WebView
  3. Android(安卓)DownloadManager下载状态查询(2)
  4. ScrollView 嵌套 RecyclerView 显示不完全问题
  5. 实战-TP数据库操作(一)
  6. CSS:媒体查询-复习
  7. ContentProvider单元测试
  8. Android(安卓)Contacts的使用(二)
  9. Android之Sqlite模糊查询

随机推荐

  1. PHP中的嵌套(多个)字符串插值
  2. ThinkPHP连接主从数据库
  3. PHP中的Xdebug会返回什么?
  4. 在Windows上安装最新的Apache 2.4.12遇到
  5. 使用Sublime Text 3的PHP 100%CPU使用率
  6. 如何使用spl_autoload_register?
  7. centos6.5下使用yum完美搭建LNMP环境(php
  8. 使用javascript将文件输入到php文件
  9. 如何使用Amazon S3创建一次性下载链接?
  10. 什么是从我的PHP网站过滤无效的utf8?