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);