mybatis嵌套collection

一:不用指定collection的column行,使用外连接查询,只能两重嵌套 


<resultMap id="groupsMap" type="im.qingtui.app.qbee.shift.controller.dto.template.service.TemplatePointDTO" >
    <id column="id" property="groupId" jdbcType="BIGINT" />
    <result column="group_name" property="groupName" jdbcType="VARCHAR" />
    <collection property="propertys" javaType="ArrayList" resultMap="groupsPointMap"/>
</resultMap>

<resultMap id="groupsPointMap" type="im.qingtui.app.qbee.shift.controller.dto.template.service.TemplatePointPropertiesDTO">
    <result property="key" column="point_key" />
    <result property="label" column="point_label" />
    <result property="unit" column="point_unit" />
</resultMap>

<!--根据模板id查询分组信息-->
<select id="selectGroupList" resultMap="groupsMap">
    select
      gp.id,
      gp.group_name,
      p.point_key,
      p.point_label,
      p.point_unit
    from
      shift_template_group_point gp
      left join template_point p on gp.id = p.group_id and p.is_delete = 0
    where
      gp.template_id = #{templateId} and gp.is_delete = 0
</select>

 

二:指定collection的column行,使用多条sql语句查询,可以实现多重嵌套

<resultMap id="groupsMap" type="im.qingtui.app.qbee.shift.controller.dto.template.service.TemplatePointDTO" >
    <id column="id" property="groupId" jdbcType="BIGINT" />
    <result column="group_name" property="groupName" jdbcType="VARCHAR" />
    //column属性,可以为多个,一个的话,可以写成column="id",多个可以写成 column="{groupId = id,xxx=xxx}"
    //column里面的字段,为主表的列,id为TemplatePointDTO对应表的列
    <collection property="propertys" column="{groupId = id}" javaType="ArrayList"
                ofType="im.qingtui.app.qbee.shift.controller.dto.template.service.TemplatePointPropertiesDTO" select="getGroupsPointMap"/>
</resultMap>
//collcetion映射的map
<resultMap id="groupsPointMap" type="im.qingtui.app.qbee.shift.controller.dto.template.service.TemplatePointPropertiesDTO">
    <result property="key" column="point_key" />
    <result property="label" column="point_label" />
    <result property="unit" column="point_unit" />
    //这种写法,可以再嵌套
</resultMap>

<!--主表查询-->
<select id="selectGroupList" resultMap="groupsMap">
    select id,group_name
    from template_group_point
    where template_id = #{templateId} and is_delete = 0
</select>

<select id="getGroupsPointMap" resultMap="groupsPointMap">
    select point_key,point_label,point_unit
    from shift_template_point
    //这里的groupId,是collection属性column
    where group_id = ${groupId} and is_delete = 0
</select>

association: 处理一对一、多对一
collection: 处理一对多

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值