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 &gt;= #{minPrice, jdbcType=INTEGER}
        </if>
        
        <!-- 小于等于 -->
        <if test="maxPrice != null and maxPrice != ' ' and minPrice==' ' ">
            AND price &lt;= #{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,所以可以用下面这种写法转义

    &lt;           < 
    &gt;          >  
    &amp;     & 
    &quot;      "

    &lt;=        <=

    &gt;=       >=

也可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析 
    <![CDATA[ 这里写你的sql ]]>  

 当然,用CDATA比较繁琐,所以还是使用转义符比较方便!

like的写法可以用下面的这种
    LIKE #param#||'%'  或 '$param$%'


 

猜你喜欢

转载自blog.csdn.net/word_joke/article/details/83827548