1、if标签语法
<select...> SQL语句1 <if test="条件表达式"> SQL语句2 </if> </select>
注意:条件表达式中大于号小于号用 gt,lt
<if test="vane gt 0">...</if>
<if test="vane lt 0">...</if>
mapper xml代码:
<select id="selectByUpdatedAt" resultMap="ResultMapWithBLOBs"> select <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from products <where> <if test="vane gt 0"> updated_at > #{date} AND status = #{status} ORDER BY is_top desc , updated_at desc </if> <if test="vane == 0"> updated_at = #{date} AND status != #{status} ORDER BY is_top desc , updated_at desc </if> <if test="vane lt 0"> updated_at < #{date} AND status = #{status} ORDER BY is_top desc , updated_at desc </if> </where> </select>
mapper 接口代码:
/** * vane大于0表示大于;0表示等于;小于0表示小于; * status 商品状态。1:在售;2:下架;3:删除; * @param vane vane * @param date 时间 * @param status 商品状态 * @return List */ List<Product> selectByUpdatedAt(@Param("vane") Integer vane, @Param("date") Date date, @Param("status") Byte status);