后台商品搜索功能开发SQL

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

版本一

<select id="SelectByNameAndParentId resultMap="Base_result" parameterType="map"">

select 

<include refid="Base_column_List"/>

from mmall_product

<if test="productId !=null">

where id = #{productId}

</if>

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

</if>

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

版本二

<select id="SelectByNameAndParentId resultMap="Base_result" parameterType="map"">

select 

<include refid="Base_column_List"/>

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 id="SelectByNameAndParentId" resultMap="BaseResult" parameterType="map">
select
<include refid="Base_column_List"/>
from mmall_product
<where>
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId !=null">
and id = #{id}
</id>
</where>
</map>

猜你喜欢

转载自www.cnblogs.com/chenligeng/p/9946123.html