Mybatis 的SQL篩選 + DataTables 資料分頁顯示
直接上程式碼: Mybatis中的 SQL價格過濾條件--直接在DataTables中生效
<!-- 商品管理-搜尋查詢 -->
<select id="listStatus" resultMap="BaseResultMap" parameterType="java.util.Map">
SELECT i.* , t.name as categoryName FROM tb_item i LEFT JOIN tb_category t ON i.cid = t.id
WHERE i.status = 1
<if test="title != null and title != ''">
AND title LIKE concat('%', #{title, jdbcType=VARCHAR}, '%') //模糊查詢
</if>
<if test="cid != 0">
AND cid = #{cid, jdbcType=INTEGER}
</if>
<!-- 大於等於 -->
<if test="minPrice != null and minPrice !=' ' and maxPrice ==' '
AND price >= #{minPrice, jdbcType=INTEGER}
</if>
<!-- 小於等於 -->
<if test="maxPrice != null and maxPrice != ' ' and minPrice==' '
AND price <= #{maxPrice, jdbcType=INTEGER}
</if>
<!-- 大於小於區間 -->
<if test="minPrice != null and maxPrice != null and minPrice!=' ' and maxPrice !=' ' ">
AND price BETWEEN #{minPrice, jdbcType=INTEGER} AND #{maxPrice, jdbcType=INTEGER}
</if>
ORDER BY price
</select>
其實就是xml的特殊符號,因為它的配置就是xml,所以可以用下面這種寫法轉義
< <
> >
& &
" "
<= <=
>= >=
也可以使用<![CDATA[ ]]>符號進行說明,將此類符號不進行解析
<![CDATA[ 這裡寫你的sql ]]>
當然,用CDATA比較繁瑣,所以還是使用轉義符比較方便!
like的寫法可以用下面的這種
LIKE #param#||'%' 或 '$param$%'