mybatis的动态sql非常强大。
其中 <where> </where> 标签 在不满足 where子句后面的筛选条件时,会去掉 where 子句。
例:
<select id="findList" resultMap="userMap" parameterType="UserDto"> select * FROM users <where> <if test ="status != null"> status = #{status} </if> <if test ="minPrice != null"> and now_price >= #{minPrice} </if> <if test="maxPrice != null"> and now_price <= #{maxPrice} </if> <if test="minTime != null"> and create_time <= #{minTime} </if> <if test="maxTime != null"> and create_time >= #{maxTime} </if> <if test="itemName != null and itemName != ''"> AND item_name LIKE concat('%',#{itemName},'%') </if> </where> </select>
以上例子中,如果where 子句中的筛选条件为空的话,将会变成下面的sql
select * from users; 就会查询所有数据。