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, 只要重复代码片段都可使用


版权声明:本文为liruidong01原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。