Mybatis 递归查询

简介:适合用于上下级关系查询,如菜单类,部门类等等

1. VO响应对象

package com.ruhuanxingyun.entity;

import lombok.Data;

import java.util.List;

/**
 * @description: 分组分支树
 * @author: ruphie
 * @date: Create in 2020/1/17 15:22
 * @company: ruhuanxingyun
 */
@Data
public class GroupHostVo {

    /**
     * ID
     */
    private Long id;

    /**
     * 父ID
     */
    private Long parentid;

    /**
     * 名称
     */
    private String name;

    /**
     * 子集
     */
    private List<GroupHostVo> children;

}

2. mapper.xml文件mysql

  <!-- 级联查询返回模型 -->
    <resultMap id="groupHostMap" type="com.ruhuanxingyun.GroupHostVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="parentId" jdbcType="BIGINT" property="parentid"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <collection column="id" property="children" ofType="com.ruhuanxingyun.GroupHostVo" select="findHostListById"/>
    </resultMap>

    <!-- 级联查询分组数据 -->
    <select id="findGroupHostTree" resultMap="groupHostMap">
        select 0 as parentId, id, `name` from group where flag = 0
    </select>

    <!-- 级联查询分支数据 -->
    <select id="findHostListById" parameterType="long" resultMap="groupHostMap">
        select sensor_id as parentId, id, `name` from host where `type` = 2 and sensor_id = #{id}
    </select>

3. swagger数据展示

猜你喜欢

转载自www.cnblogs.com/ruhuanxingyun/p/12206488.html