首页 > 其他分享 >MybatisPlus多表连接查询一对多分页查询数据

MybatisPlus多表连接查询一对多分页查询数据

时间:2022-12-01 13:31:39浏览次数:63  
标签:11 deptId MybatisPlus 05 gmtCreate private 查询 多表 gmtModified

一、序言

在日常一线开发过程中,多表连接查询不可或缺,基于MybatisPlus多表连接查询究竟该如何实现,本文将带你找到答案。

在多表连接查询中,既有查询单条记录的情况,又有列表查询,还有分页查询,这些需求与多表连接是什么关系,又该如何实现,这是本文讨论的中心内容。

二、实战编码

1、两个关联DO

部门DO

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_dept")
public class Dept {
    private static final long serialVersionUID = 1L;
    @TableId(type = IdType.AUTO)
    private Long deptId;
    private String deptName;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreate;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModified;
    private String tel;
    
    public Dept(Dept dept) {
        if (Objects.nonNull(dept)) {
            this.deptId = dept.deptId;
            this.deptName = dept.deptName;
            this.gmtCreate = dept.gmtCreate;
            this.gmtModified = dept.gmtModified;
            this.tel = dept.tel;
        }
    }
}

用户DO

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_user")
public class User {
    private static final long serialVersionUID = 1L;
    private Integer age;
    private Long deptId;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreate;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModified;
    @TableId(type = IdType.AUTO)
    private Long userId;
    private String userName;
    
    public User(User user) {
        if (Objects.nonNull(user)) {
            this.age = user.age;
            this.deptId = user.deptId;
            this.gmtCreate = user.gmtCreate;
            this.gmtModified = user.gmtModified;
            this.userId = user.userId;
            this.userName = user.userName;
        }
    }
}
2、部门VO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeptVo extends Dept {
    private List<User> userList;
    /**
     * 实现部门DO 转 部门VO
     * @param dept
     */
    public DeptVo(Dept dept) {
        super(dept);
    }
}
3、普通编码查询数据
public IPage<DeptVo> selectDeptPage3() {
    LambdaQueryWrapper<Dept> wrapper = Wrappers.lambdaQuery(Dept.class);
    Page<Dept> deptPage = this.page(new Page<>(1, 3), wrapper);
    IPage<DeptVo> deptVoPage = EntityUtils.toPage(deptPage, DeptVo::new);
    // 完成userList字段属性注入
    Set<Long> deptIds = EntityUtils.toSet(deptVoPage.getRecords(), DeptVo::getDeptId);
    if (deptIds.size() > 0) {
        List<User> userList = userMapper.selectList(Wrappers.lambdaQuery(User.class)
                                                    .in(User::getDeptId, deptIds));
        Map<Long, List<User>> map = EntityUtils.groupBy(userList, User::getDeptId);
        for (DeptVo deptVo : deptVoPage.getRecords()) {
            deptVo.setUserList(map.get(deptVo.getDeptId()));
        }
    }
    return deptVoPage;
}
4、使用工具类查询数据

优化版 一行代码完成userList属性注入

/**
 * 优化版 一行代码完成userList属性注入
 */
@Override
public IPage<DeptVo> selectDeptPage4() {
    LambdaQueryWrapper<Dept> wrapper = Wrappers.lambdaQuery(Dept.class);
    Page<Dept> deptPage = this.page(new Page<>(1, 3), wrapper);
    IPage<DeptVo> deptVoPage = EntityUtils.toPage(deptPage, DeptVo::new);
    //  一行代码完成userList属性注入
    FieldInjectUtils.injectListField(deptVoPage, DeptVo::getDeptId, UserServiceImpl.class, User::getDeptId, DeptVo::getUserList);
    return deptVoPage;
}

需要指出的是FieldInjectUtils在工具包下

<dependency>
    <groupId>xin.altitude.cms</groupId>
    <artifactId>ucode-cms-common</artifactId>
    <version>1.5.9.2</version>
</dependency>

学习源码的朋友,源码直通车

5、演示数据
{
  "code": 200,
  "msg": "操作成功",
  "data": {
    "records": [
      {
        "deptId": "10",
        "deptName": "Java",
        "gmtCreate": "2020-10-30 11:48:19",
        "gmtModified": "2021-05-24 15:11:17",
        "tel": "88886666",
        "userList": [
          {
            "age": 12,
            "deptId": "10",
            "gmtCreate": null,
            "gmtModified": "2022-11-05 16:44:22",
            "userId": "1",
            "userName": "Jone"
          }
        ]
      },
      {
        "deptId": "11",
        "deptName": "Mysql",
        "gmtCreate": "2020-10-30 11:48:44",
        "gmtModified": "2021-05-24 15:11:20",
        "tel": "80802121",
        "userList": [
          {
            "age": 23,
            "deptId": "11",
            "gmtCreate": null,
            "gmtModified": "2022-11-05 16:44:24",
            "userId": "2",
            "userName": "Jack"
          },
          {
            "age": 21,
            "deptId": "11",
            "gmtCreate": "2022-11-05 16:09:42",
            "gmtModified": "2022-11-05 16:11:28",
            "userId": "5",
            "userName": "滴滴"
          }
        ]
      },
      {
        "deptId": "12",
        "deptName": "Tomcat",
        "gmtCreate": "2020-10-30 11:48:44",
        "gmtModified": "2021-05-24 15:11:23",
        "tel": "23231212",
        "userList": [
          {
            "age": 22,
            "deptId": "12",
            "gmtCreate": null,
            "gmtModified": "2022-11-05 16:44:27",
            "userId": "3",
            "userName": "Billie"
          },
          {
            "age": 12,
            "deptId": "12",
            "gmtCreate": "2021-06-05 19:22:46",
            "gmtModified": "2021-10-21 14:38:26",
            "userId": "4",
            "userName": "didi"
          },
          {
            "age": 18,
            "deptId": "12",
            "gmtCreate": "2022-11-05 16:10:48",
            "gmtModified": "2022-11-05 16:11:36",
            "userId": "6",
            "userName": "嗒嗒"
          }
        ]
      }
    ],
    "total": 4,
    "size": 3,
    "current": 1,
    "orders": [],
    "optimizeCountSql": true,
    "searchCount": true,
    "countId": null,
    "maxLimit": null,
    "pages": 2
  }
}

三、小结

本文完成了MybatisPlus一对多分页查询数据的开发需求,更多细节内容,视频直通车。

标签:11,deptId,MybatisPlus,05,gmtCreate,private,查询,多表,gmtModified
From: https://blog.51cto.com/u_15495434/5901829

相关文章

  • MyBatis条件查询
    多条件查询select*fromUser<where><if"name!=nullandname!=''">andnamelike#{name}<if><if"age!=nullandag......
  • MySQL进阶实战7,查询的执行过程
    一、拆分查询将大查询拆分成小查询,每个查询功能完全一样,只是一小部分,每次只返回一小部分查询结果。比如在删除旧数据的时候,如果用一个大的语句一次性删的话,则可能需要一次性......
  • MySQL进阶实战5,为什么查询速度会慢
    一、先了解一下MySQL查询的执行过程MySQL在查询时,它是由很多子任务组成的,每个子任务都会消耗一定的时间,如果要想优化查询,实际上要优化其子任务,可以消除一些子任务、减少子......
  • 没用好mybatisplus的Wrapper,我真难为情啊
    背景我们的springboot应用程序的持久层,是用jeecgboot框架生成的代码。mybatisplus版本是3.1.2。 在一次对当前程序的sql性能优化时,我重写了BaseMapper的selectPage方法......
  • 模型查询
    目录基础查询)模糊查询进阶查询集合函数分组原生sql基础查询)all查询所有对象(表里的每一条记录就是一个模型类对象),返回queryset对象(queryset是一个集合他是一个数据......
  • mybatis 连接 oracle使用concat关键字模糊查询
    oracle中不支持concat的三个参数的拼接,需要更正为SELECT*FROM"t_Dormitorys"where"RoomName"likeCONCAT(CONCAT('%','1'),'%')......
  • DQL-20课 自连接笔记及查询练习-2022-11-30
    自连接自己的表和自己连接一张表拆成两张一样的表查询父子信息 (了解即可)--学号、姓名、年级名字SELECT`studentno`,`studentname`,`gradename`FROMstudentsINNER......
  • DQL-查询笔记-2022-11-30
    联表查询join连接的表on(判断的条件)连接查询  固定的语法where 等值查询  分析需求 分析查询的数据来源于那些表确定使用那种连接7种确认交叉点(这两张......
  • mysql 反向like 查询 匹配关键字 回复
    场景:后台设置关键字用户发送内容去匹配关键字自动回复sql:SELECT*FROMi_auto_replyWHERE'你好!什么时候发货'LIKECONCAT('%',`key`,'%') 结果......
  • Mybatis源码分析(十三) - 关联查询之多对多
    我的理解是,多对多其实就是两个一对多。嵌套结果:示例代码:<selectid="selectUserRole"resultMap="userRoleInfo">selecta.id,a.user_name,a.real......