Mybatis递归查询树型目录

可参考链接:https://blog.csdn.net/lianzhang861/article/details/86243532

  • 效果图
    Mybatis递归查询树型目录_第1张图片
  • 数据库表
    Mybatis递归查询树型目录_第2张图片
  • 自定义数据返回值类型

<resultMap id="user_group_dto" type="org.hzero.iam.api.controller.v1.dto.UserGroupDTO">
        <id column="user_group_id" property="userGroupId" javaType="java.lang.Long"/>
        <result column="group_code" property="groupCode"/>
        <result column="group_name" property="groupName"/>
        <result column="remark" property="remark"/>
        <result column="enabled_flag" property="enabledFlag"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="group_level" property="groupLevel"/>
        <result column="level_path" property="levelPath"/>
        <result column="order_seq" property="orderSeq"/>
        
        <collection column="user_group_id" property="children"
                    ofType="org.hzero.iam.api.controller.v1.dto.UserGroupDTO"
                    select="org.hzero.iam.infra.mapper.IamUserGroupMapper.selectChildUserGroup">
        collection>
    resultMap>
  • sql查询

<select id="selectUserGroupByGroupLevel" parameterType="entity.UserGroup" resultMap="user_group_dto">
    SELECT
        hug.user_group_id,
        hug.group_code,
        hug.group_name,
        hug.enabled_flag,
        hug.tenant_id,
        hug.remark,
        ht.tenant_name,            
        hug.group_level,
        hug.level_path,
        hug.order_seq,
        hug.parent_group_id
    FROM hiam_user_group hug
    LEFT JOIN hpfm_tenant ht ON hug.tenant_id = ht.tenant_id
    <where>
        hug.group_level = #{groupLevel}
        <if test="userGroupId != null">
            AND hug.user_group_id = #{userGroupId}
        if>
        
    where>
    ORDER BY hug.creation_date DESC
select>



<select id="selectChildUserGroup" resultMap="user_group_dto">
    select
        hug.user_group_id,
        hug.group_code,
        hug.group_name,
        hug.enabled_flag,
        hug.tenant_id,
        hug.remark,
        ht.tenant_name,
        
        hug.group_level,
        hug.level_path,
        hug.order_seq,
        hug.parent_group_id
    FROM hiam_user_group hug
    LEFT JOIN hpfm_tenant ht ON hug.tenant_id = ht.tenant_id
    <where>
    	
        parent_group_id = #{userGroupId}          
    where>
select>

你可能感兴趣的:(学习)