1. 程式人生 > >Mybatis if標籤判斷數字大小

Mybatis if標籤判斷數字大小

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 &gt; #{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 &lt; #{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);