MyBatis - 两种查询树形数据(最多三级结构)

@Data
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class OrgTree implements Serializable {

    private String id;

    private String label;

    private List<OrgTree> children;

}

<resultMap type="com.dto.OrgTree" id="BaseResultMap">
        <id column="id" property="id"/>
        <result column="label" property="label"/>
        <collection property="children" ofType="com.dto.OrgTree">
            <id column="id2" property="id"/>
            <result column="label2" property="label"/>
            <collection property="children" ofType="com.dto.OrgTree">
                <id column="id3" property="id"/>
                <result column="label3" property="label"/>
            </collection>
        </collection>
    </resultMap>
    
<select id="getOrgTree" resultMap="BaseResultMap">
        select
            m1.org_code as id,
            m1.org_name as label,
            m2.org_code as id2,
            m2.org_name as label2,
            m3.org_code as id3,
            m3.org_name as label3
        from org_table m1,org_table m2,org_table m3
        where
			m1.org_code=m2.parent_org_code
			and
		    m2.org_code=m3.parent_org_code

        <!--可以根据不同用户的级别查询当前用户的组织-->
        <if test="firstCode != '' and firstCode != null ">
            and m1.org_code=#{firstCode}
        </if>
        <if test="secondCode  != '' and secondCode != null ">
            and m2.org_code=#{secondCode}
        </if>
        <if test="thirdCode != '' and thirdCode != null ">
            and m3.org_code=#{thirdCode}
        </if>


	  order by id,id2,id3
    </select>
	

MyBatis - 两种查询树形数据的方法详解(嵌套结果集、递归查询)

参考连接

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值