这篇文章主要讲解了“mybatisPlus如何实现对象嵌套关联查询一对多List集合查询”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MybatisPlus如何实现对象嵌套关联查询一对多Li
这篇文章主要讲解了“mybatisPlus如何实现对象嵌套关联查询一对多List集合查询”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MybatisPlus如何实现对象嵌套关联查询一对多List集合查询”吧!
由于我的是一对集合查询,所以我有两个类。
@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配置就可以了
查询班级表 返回所有学生信息 (一对多问题)
班级class_info
学生student
<!-- 多对一 或者 一对一 --><!-- <association property=""--><!-- 一对多 返回集合--><!- - <collection property=""- ->
实体类ClassInfo.java
@Datapublic 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
@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 { @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集合查询这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: MybatisPlus如何实现对象嵌套关联查询一对多List集合查询
本文链接: https://lsjlt.com/news/329908.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0