温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MybatisPlus如何实现对象嵌套关联查询一对多List集合查询

发布时间:2022-05-23 09:36:03 来源:亿速云 阅读:2481 作者:iii 栏目:开发技术

这篇文章主要讲解了“MybatisPlus如何实现对象嵌套关联查询一对多List集合查询”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MybatisPlus如何实现对象嵌套关联查询一对多List集合查询”吧!

对象嵌套关联查询一对多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
@TableName
public 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配置就可以了 

一对多查询(经典案例)

条件

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

数据库

班级class_info

MybatisPlus如何实现对象嵌套关联查询一对多List集合查询

学生student

MybatisPlus如何实现对象嵌套关联查询一对多List集合查询

代码实现

<!--        多对一  或者 一对一   -->
<!--        <association property=""-->
<!--        一对多 返回集合-->
<!- -  <collection  property=""- ->

实体类ClassInfo.java

@Data
public class ClassInfo {
 
    private Long id;
    private String name;
    private String nameTest; 
    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">
 
    <!--    查询班级 返回所有学生的信息   一对多-->
 
    <!--    自定义映射规则-->
    <resultMap id="OneToMany" type="com.qcby.zsgc.entity.ClassInfo">
        <result column="name" jdbcType="VARCHAR" property="nameTest" />
        <collection  column="{id1=id,name=name}"
                     property="studentList"
                     select="com.example.demo.mapper.StudentMapper.listByClassInfoId">                 </collection>
    </resultMap>
 
    <select id="listAllWithStudent" resultMap="OneToMany">
        select * from class_info
    </select>

关联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

@Service
public 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 {
 
    @Autowired
    private ClassInfoService classInfoService;
 
    @RequestMapping("listAllWithStudent")
    @ResponseBody
    public IPage<ClassInfo> listAllWithStudent(Integer pageNo,Integer pageSize){
        Page<ClassInfo> page = new Page<>(pageNo,pageSize);
        return classInfoService.listAllWithStudent(page);
    } 
}

感谢各位的阅读,以上就是“MybatisPlus如何实现对象嵌套关联查询一对多List集合查询”的内容了,经过本文的学习后,相信大家对MybatisPlus如何实现对象嵌套关联查询一对多List集合查询这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI