总之先上一张图:
从我用到的开始弄:
include标签引用,可以复用SQL片段
sql标签中id属性对应include标签中的refid属性。通过include标签将sql片段和原sql片段进行拼接成一个完整的sql语句进行执行。
res_type_id,res_type select from pub_res_type 引用同一个xml中的sql片段 引用公用的sql片段 include标签中也可以用property标签,用以指定自定义属性。在sql标签中通过${}取出对应的属性值。<select id="queryPubResType" parameterType="com.fist.store" resultMap="PubResTypeList">
select a.storeid,
<include refid="comm.auth_param_sql3">
<property name="storeid" value="a.storeid"/>
<property name="lng" value="#{lngId}"/>
<property name="female" value="'女'"/>
</include> as res_type
from tb_pub_staffstore a
</select>
<sql id="auth_param_sql3">
<if test="storeListForRole != null and storeListForRole.size()>0">
and ${storeid} in
<foreach collection="storeListForRole" item="storeid" open="(" close=")" separator=",">
#{storeid}
</foreach>
</if>
<if test="lngId != null ">
and ${lng} = #{lngId}
</if>
<if test="sex!= null ">
and ${female} = #{sex}
</if>
</sql>
mapper-collection标签的嵌套
在collection标签中直接使用collection表签会无法映射到实体字段,解决办法是把collection装到另一个resultMap里面去;为何这样设计?奇怪的是,我在搜索资料时发现其他人有这样写(双层collection)的。
正例:
<resultMap>
<collection property="storeCouponSuitable" javaType="java.util.ArrayList"
resultMap="storeCouponSuitableResultMap">
</collection>
</resultMap>
<resultMap id="storeCouponSuitableResultMap" type="com.firstouch.modules.activity.service.dto.StoreCouponSuitableDetailDto">
<result property="suitableRange" column="suitable_range" jdbcType="INTEGER"/>
<collection property="commodityList" resultMap="com.firstouch.modules.category.service.mapper.StoreCategoryMapper.storeCategoryResponse"/>
<collection property="categoryList" resultMap="com.firstouch.modules.product.service.mapper.StoreProductMapper.storeProductResponse"/>
</resultMap>
反例:collection 内的两个collection 都无法映射到实体字段,编译报错;
<collection property="storeCouponSuitable" javaType="java.util.ArrayList">
<result property="suitableRange" column="suitable_range" jdbcType="INTEGER"/>
<collection property="commodityList" resultMap="com.firstouch.modules.category.service.mapper.StoreCategoryMapper.storeCategoryResponse"/>
<collection property="categoryList" resultMap="com.firstouch.modules.product.service.mapper.StoreProductMapper.storeProductResponse"/>
</collection>
引用其他resultMap
<collection property="categoryList" columnPrefix="category_"
resultMap="com.firstouch.modules.category.service.mapper.StoreCategoryMapper.storeCategoryResponse"/>
<resultMap type="com.firstouch.modules.category.service.dto.YxStoreCategoryResponseDto" id="storeCategoryResponse">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="pid" column="pid" jdbcType="INTEGER"/>
<result property="cateName" column="cate_name" jdbcType="VARCHAR"/>
<result property="pic" column="pic" jdbcType="VARCHAR"/>
<result property="isShow" column="is_show" jdbcType="INTEGER"/>
</resultMap>
不定时更新,如有错误欢迎各位大佬指出讨论
本文介绍了MyBatis中`include`标签用于复用SQL片段的使用方法,以及`mapper-collection`标签的嵌套问题。在处理嵌套时,直接使用`collection`标签可能会导致实体字段映射失败,解决方案是将其放入另一个`resultMap`中。同时,文章还提到了错误的示例——双层`collection`会导致编译错误。最后,作者提到会不定时更新,并欢迎读者指出错误进行讨论。
2184

被折叠的 条评论
为什么被折叠?



