springBoot学习笔记(2.2)—— 整合mybatis之递归子查询
前言
提示:前两篇文章研究了spring Boot整合mybaits,现在这篇文章研究mybatis中递归子查询的问题。至于编写application.yml配置文件此次不再赘述
一、搭建步骤
1.构建实体类和表结构
@Datapublic class Dept {/*** 主键id*/private Long id;/*** 部门名称*/private String deptName;/*** 父类id*/private Long parentId;/*** 排序码*/private String orderCode;/*** 是否开启使用(0否,1是)*/private Integer isStart;/*** 说明*/private String description;/*** 地址*/private String address;/*** 人员数据*/private List<User> userList;}
CREATE TABLE dept(id BIGINT (15) NOT NULL AUTO_INCREMENT COMMENT '主键id',dept_name VARCHAR (50) NOT NULL DEFAULT '' COMMENT '部门名称',parent_id VARCHAR (50) NOT NULL DEFAULT '' COMMENT '父类id',order_code VARCHAR (50) NOT NULL DEFAULT '' COMMENT '排序码',is_start INT (11) NOT NULL DEFAULT 0 COMMENT '是否开启使用(0否,1是)',description VARCHAR (50) NOT NULL DEFAULT '' COMMENT '说明',address VARCHAR (50) NOT NULL DEFAULT '' COMMENT '地址',PRIMARY KEY (id))ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'dept';
部门表根据id和parent_id 相互关联,表数据可以自己构建,此处不单独引入。
2.Dao接口详情
* description: 递归查询树状数据* version: 1.0* date: 2021/12/31 15:09* author: xiaYZ* iteration: 迭代说明* @param* @return*/List<Dept> findDeptTreeData();
3. Mapper文件内容 (重点)
<resultMap id="deptTreeMap" type="com.example.springbootmybatis.entity.Dept"><id column="id" property="id"/><result column="dept_name" property="deptName"/><result column="parent_id" property="parentId"/><result column="order_code" property="orderCode"/><result column="is_start" property="isStart"/><result column="description" property="description"/><result column="address" property="address"/><!--递归子查询,findDeptTreeData方法中传入的id,为findChildren中的入参--><collection property="children" column="id" ofType="com.example.springbootmybatis.entity.Dept" select="findChildren"/></resultMap><select id="findDeptTreeData" resultMap="deptTreeMap">select d.id, dept_name, parent_id, order_code, is_start, description, addressfrom dept d where parent_id = 0</select><!--如果只需要查询两级分类数据的话可以不使用resultMap="deptTreeMap",使用Dept类即可--><select id="findChildren" resultMap="deptTreeMap">select d.id, dept_name, parent_id, order_code, is_start, description, addressfrom dept dwhere parent_id = #{id}</select>
4.service层方法
public List<Dept> findDeptTreeData(){return deptDao.findDeptTreeData();}
5.controller层方法
/**** description: 查询部门列表树状数据* version: 1.0 ->* date: 2021/12/31 15:19* author: xiaYZ* iteration: 迭代说明* @param* @return java.util.List<com.example.springbootmybatis.entity.Dept>*/@GetMapping("findDeptTreeData")public List<Dept> findDeptTreeData() {List<Dept> deptList = new ArrayList<>();try{deptList = deptService.findDeptTreeData();}catch (Exception e){e.printStackTrace();}return deptList;}
6.运行截图

注意:
- 使用此种方法可能会增大数据压力,一个方法会递归查询所有部门数据。
- 如果此处不想让数据库压力过大,可以先把所有部门数据查出来再通过递归方法构建出树状数据类型。
二、使用递归算法构建树状数据
1.mapper内容
<!--查询所有部门数据--><select id="findAllDeptData" resultMap="deptMap">select d.id, dept_name, parent_id, order_code, is_start, description, addressfrom dept d</select>
2.service层中算法
public List<Dept> recursionFindDeptTreeData(){List<Dept> allDeptData = deptDao.findAllDeptData();return recursionWay(allDeptData, 0L);}/*** 获取子类递归算法*/public List<Dept> recursionWay(List<Dept> deptList,Long parentId){List<Dept> childrenDept = new ArrayList<>();for(Dept sonDept : deptList){if(Objects.equals(parentId,sonDept.getParentId())){childrenDept.add(sonDept);sonDept.setChildren(recursionWay(deptList,sonDept.getId()));}}if(CollectionUtil.isEmpty(deptList)){return null;}return childrenDept;}
总结
- 使用collection标签递归查询子类数据。
- 或者先查询所有数据,再使用递归子类算法查询树状数据。
