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
0
投稿
猜你喜欢
- 先看MVC模式流程图(其实MVC设计模式就是java中的model2。): &nb
- java web 实现分页功能,分享给大家,具体如下:使用框架:ssm数据库:oracle话说 oracle 的分页查询比 mysql 复杂
- 前言spring中解析元素最重要的一个对象应该就属于 BeanDefinition了;这个Spring容器中最基本的内部数据结构;它让xml
- Condition的作用是对锁进行更精确的控制。Condition中的await()方法相当于Object的wait()方法,Conditi
- 一.什么是多渠道打包在不同的应用市场可能有不同的统计需求,需要为每个应用市场发布一个安装包,这里就引出了Android的多渠道打包。在安装包
- 开发环境win10Android Studio效果用于多级菜单展示,或选择。如 每个省,市,县;如 树木的病虫害;关键代码 @overrid
- 重要属性1、scale: 小数点后的位数。如将1.234构建为BigDecimal,scale属性则为32、RoundingMode(对照数
- 前言不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息。获取用户信息就是获取公众号下微信用户的
- 去除XSS字符串需要借助工具类 jsoup ,这里jsoup有一点需要注意的是,jsoup的功能可能有点太强大了,能把xss攻击的内容直接过
- Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。有时候我们之直接调用Win32 的AP
- springboot 中各种配置项纪录1. @Value最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言。// 使用起
- Java泛型是JDK 5引入的一个特性,它允许我们定义类和接口的时候使用参数类型,泛型在集合框架中被广泛使用。类型擦除是泛型中最让人困惑的部
- 前言回老家,实在太无聊,于是乎给自己整了一套台式机配置,总价 1W+,本以为机器到位后可以打打游戏,学学技术打发无聊的时光。但是我早已不是从
- 一个Java程序的执行要经过编译和执行(解释)这两个步骤,同时Java又是面向对象的编程语言。当子类和父类存在同一个方法,子类重写了父类的方
- 最近看到一道有点意思的逻辑算法题,便着手实现一下。题目是要求打印 出N*N顺时针螺旋数组,规律如下:// 1 2 &
- 一、引言在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文
- 本人所使用的开发环境是VS2008,开发的系统所在移动终端版本为windows mobile 5.0。由于需要进行身份的验证,需要获取移动终
- 本文实例讲述了Android开发实现SubMenu选项菜单和子菜单。分享给大家供大家参考,具体如下:简介:SubMenu:代表一个子菜单,包
- 这篇文章主要介绍了Spring Cloud基于zuul实现网关过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- Comparable 比较器,内置定义的比较方法,实现比较 较简单Comparator 策略模式,需要定义不同的策略和比较的对象,实现比较