在做后台的商品搜索功能开发时遇到了一些问题记录下来

版本一

<select >

select 

<include ref/>

from mmall_product

<if test="productId !=null">

where id = #{productId}

</if>

<if test="productName !=null">
and name like {productName}

</if>

这个是有问题的如果第一个if没有传值过来,那么第二个if就不是正确的sql语句

版本二

<select >

select 

<include ref/>

from mmall_product

where 1 = 1

<if test="productId !=null">

and id = #{productId}

</if>

<if test="productName !=null">
and name like {productName}

</if>

</select>

这个没问题,但是为了看起来顺眼我么使用了<where>标签

最终版本
<select >
and id = #{id}
</id>
</where>
</map>

相关文章: