mybaits 递归层级(父子)查询

实体:chargeItem
属性:ics_charge_item_pk //主键
ics_charge_item_name //名称
parent_charge_item_pk //上级主键

递归查询,查询出来的DTO

//返回的结果实体

ChargeItemDTO

private Long ics_charge_item_pk;   //主键
private String ics_charge_item_name ;//名称
private String parent_charge_item_pk;//上级pk
private List<ChargeItemDTO> children = new ArrayList<ChargeItemDTO>;//子数据

mybaits xml配置文件

//1.定义返回resultMap
<resultMap id="chargeItemSelectMap" type="com.splbilling.app.dto.IcsChargeItemSelectDTO">
        <id column="ics_charge_item_pk" jdbcType="BIGINT" property="chargeItemPk"/>
<result column="charge_item_name" jdbcType="VARCHAR" property="chargeItemName"/>
        <collection property="children" select="getSubChargeItem" column="{parentPk=ics_charge_item_pk}"></collection>
</resultMap>
2.定义查询父的方法
  <select id="getAllChargeItems" resultMap="chargeItemSelectMap">
        SELECT ics_charge_item_pk,charge_item_name
        FROM ics_charge_item
        WHERE parent_charge_item_pk IS NULL
    </select>
3.定义查询子的方法
<select id="getSubChargeItem" resultMap="chargeItemSelectMap" parameterType="java.util.Map">
        SELECT ics_charge_item_pk,charge_item_name
        FROM ics_charge_item
        WHERE parent_charge_item_pk = #{parentPk}
</select>

注:
1.定义返回的resultMap 里面的 collection 里的column的parentPk = ics_charge_item_pk
2.定义两个方法 方法一:查询最上级的对象 ;方法二:查询子对象

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值