MybatisPlus实现对象嵌套关联查询一对多List集合查询
作者:weixin_52366309 发布时间:2022-04-27 16:00:12
标签:对象嵌套,一对多,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
学生student
代码实现
<!-- 多对一 或者 一对一 -->
<!-- <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);
}
}
来源:https://blog.csdn.net/weixin_52366309/article/details/114293133


猜你喜欢
- 普通 jar 包的导出1.点击 file 中的project.structor=>选择Artifacts=>+=>选择 j
- 本文实例讲述了C#实现多线程下载文件的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co
- RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应
- 1 关于自动内存管理Java是由jvm来管理内存,包括自动分配以及自动回收,因此它不容易出现内存泄漏和内存溢出问题。C/C++,由程序员手动
- VC和BCB中做一个Server的监听程序,只需要指定端口,然后监听(Listen)就行了.在C#找不到这个函数了,慢慢看MSDN,怎么需要
- 目录对象的创建创建方式对象的内存布局创建过程对象的内存分配分配方式并发安全代码优化逃逸分析的不成熟性实际的对象空间分配过程对象的访问句柄直接
- C语言实现矩阵运算给定一个n×n的方阵,本题要求计算该矩阵除副对角线、最后一列和最后一行以外的所有元素之和。副对角线为从矩阵的右上角至左下角
- jdk下载并配置下载jdk下图是自己资源管理器中jdk的安装路径,双击然后next就好,不需要改什么配置手里没有安装包的,下载地址在这里 :
- 宏定义与预处理命令预处理阶段:处理宏定义与预处理命令;编译期:检查代码,分析语法、语义等,最后生成.o或.obj文件;链接期:链接所有的.o
- 使用 AppbarLayout 和 MotionLayout 实现常用的布局效果前文我们讲了协调滚动的一些定义方式,我们在开发中常用的几种效
- Redis是一个缓存消息中间件及具有丰富特性的键值存储系统。Spring Boot为Jedis客户端库和由Spring Data Redis
- 前提:已经配置Zuul网关参考:https://www.jb51.net/article/182894.htm限流方式:1)nginx层限流
- 之前项目总会遇到很多搜索框类的功能,虽然不是很复杂,不过每次都要去自己处理数据,并且去处理搜索框的变化,写起来也比较麻烦,今天来做一个比较简
- 本文实例为大家分享了java使用influxDB数据库的具体代码,供大家参考,具体内容如下1.pom.xml中导入jar包依赖<!--
- 在系统开发中,需要对请求和响应分别拦截下来进行解密和加密处理,在springboot中提供了RequestBodyAdviceAdapter
- 本文实例为大家分享了Android ViewPager实现页面左右切换的具体代码,供大家参考,具体内容如下主界面viewpager.xml:
- public class PullToLoadListView extends ListView implements OnScrollLi
- 线程中start方法与run方法的区别在线程中,如果start方法依次调用run方法,为什么我们会选择去调用start方法?或者在java线
- 如下所示:class Program {
- 在intellij中忽略提交文件,分两种情况,文件没有纳入版本管理第一种方法文件还没有纳入版本管理,这种通过 svn的ignore配置ver