springBoot学习笔记(2.2)—— 整合mybatis之递归子查询

更多文章

更多系列文章在个人网站

springBoot学习系列笔记文章

springBoot学习笔记(1)—— 搭建springBoot项目

提示:



前言

提示:前两篇文章研究了spring Boot整合mybaits,现在这篇文章研究mybatis中递归子查询的问题。至于编写application.yml配置文件此次不再赘述


一、搭建步骤

1.构建实体类和表结构

@Data
public 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, address
        from 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, address
        from dept d
        where 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. 使用此种方法可能会增大数据压力,一个方法会递归查询所有部门数据。
  2. 如果此处不想让数据库压力过大,可以先把所有部门数据查出来再通过递归方法构建出树状数据类型。

二、使用递归算法构建树状数据

1.mapper内容

<!--查询所有部门数据-->
    <select id="findAllDeptData" resultMap="deptMap">
        select d.id, dept_name, parent_id, order_code, is_start, description, address
        from 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;
    }

总结

  1. 使用collection标签递归查询子类数据。
  2. 或者先查询所有数据,再使用递归子类算法查询树状数据。

项目源码

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值