在mapper.xml文件中如果有多个查询条件是相同的,可以提取处相同部分,封装起来,哪里使用哪里调用
eg:
select * from product where 1=1
<if test="productNo != null and productNo != ''">
and p.productNo like '%${productNo}%'
</if>
<if test="productName != null and productName != ''">
and p.productName like '%${productName}%'
</if>
<if test="istatus == null">
and o.istatus >= 0
</if>
<if test="startDate != null">
and o.CreateTime >= #{startDate}
</if>
<if test="endDate != null">
and o.CreateTime < #{endDate};
select count(*) from product where 1=1
<if test="productNo != null and productNo != ''">
and p.productNo like '%${productNo}%'
</if>
<if test="productName != null and productName != ''">
and p.productName like '%${productName}%'
</if>
<if test="istatus == null">
and o.istatus >= 0
</if>
<if test="startDate != null">
and o.CreateTime >= #{startDate}
</if>
<if test="endDate != null">
and o.CreateTime < #{endDate};
上面两个sql中用到的查询条件相同,所以我们提取相同查询条件
<sql id="common_where_if">
<if test="productNo != null and productNo != ''">
and p.productNo like '%${productNo}%'
</if>
<if test="productName != null and productName != ''">
and p.productName like '%${productName}%'
</if>
<if test="istatus == null">
and o.istatus >= 0
</if>
<if test="startDate != null">
and o.CreateTime >= #{startDate}
</if>
<if test="endDate != null">
and o.CreateTime < #{endDate}
</sql>
在sql中使用查询条件
<select id="selectList" resultType="com.dims.mes.order.vo.MopOrderVo"
parameterType="java.util.Map">
select * from product
<where>
<include refid="common_where_if"/>
</where>
</select>