MyBatis sql xml include标签使用 (代码去重)
·
MyBatis sql xml 对于重复的代码片段 可以使用 include 标签进行优化
例如 以下将查询条件进行优化:
<sql id="where"><!-- 定义代码片段-->
<where>
<if test="CarNo != null and CarNo != ''">
and car_no like concat('%',#{CarNo},'%')
</if>
<if test="CardNo != null and CardNo != ''">
and card_no = #{CardNo}
</if>
<if test="serviceType != null and serviceType != ''">
and service_type = #{serviceType}
</if>
.....省略
</where>
</sql>
<select id="queryList" resultType="com.framework.entity.WeightEntity">
select * from weight
<include refid="where"/> <!-- 引用-->
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>
</select>
<select id="queryTotal" resultType="int">
select count(*) from weight
<include refid="where" /> <!-- 引用->
</select>
当然 include标签不仅仅用于where, 只要重复代码片段都可使用
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)