mybatis一对多关系

1. 写一个正常的sql 查询自己想要的数据的外层

 

  <resultMap id="getChild" type="com.kx.whpcomp.oem.application.dangeroem.entity.DangerOemTemplateLink" >
        <id column="id" property="id"/>
        <result column="danger_check_group_id" property="dangerCheckGroupId"/>
    </resultMap>
 
    <select id="getCheckItem" parameterType="java.util.Map" resultMap="getChild">
        select t.*,tt.danger_range dangerCheckGroupName
        from t_whp_danger_oem_template_link t
        left join t_whp_danger_oem_check_group tt on t.danger_check_group_id = tt.id
        where 1=1
        <if test="pd.templateId != null and pd.templateId != '' ">
            and t.danger_template_id = #{pd.templateId}
        </if>
        GROUP BY t.danger_check_group_id
    </select>

2. 实体类中增加子集的集合

3. 返回resultmap的结果中增加collection

 property : 属性名( 2 中的属性名)

javaType:属性名的类型

column: 配置查询sql的参数名( qq: 查询子集的参数,danger_check_group_id:1 中sql查询出的字段名)

select: 查询的方法名

ofType:查询子集的实体类(包含全路径)

 <resultMap id="getChild" type="com.kx.whpcomp.oem.application.dangeroem.entity.DangerOemTemplateLink" >
        <id column="id" property="id"/>
        <result column="danger_check_group_id" property="dangerCheckGroupId"/>
        <collection  property="checkItem" javaType="ArrayList" column="{qq=danger_check_group_id}" select="getCheckItems"  ofType="com.kx.whpcomp.oem.application.dangeroem.entity.DangerOemCheckItem">
        </collection>
    </resultMap>

 <resultMap id="itemBase" type="com.kx.whpcomp.oem.application.dangeroem.entity.DangerOemCheckItem" ></resultMap>


  <select id="getCheckItems" parameterType="java.util.Map" resultMap="itemBase">
        select * from t_whp_danger_oem_check_item a where a.danger_group_id = #{qq}
    </select>

4. 实现 3 中的查询方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值