Mybatis-Plus 利用limit 分頁


這里記錄一下個人犯過的錯誤。

首先是寫一個不能執行的代碼:

<select id="getAll" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM t_menu
        <where>
            <if test="title != null and title != ''">
                and  title like concat('%', #{title}, '%')
            </if>
            <if test="deleted != null and deleted != ''">
                and  deleted =  #{deleted}
            </if>
            <if test="dateStar != null and dateStar != ''">
                and gmt_create &gt;=#{dateStar}
            </if>
            <if test="dateEnd != null and dateEnd != ''">
                and gmt_create &lt;=#{dateEnd}
            </if>
        </where>
        ORDER BY pid ASC
        <if test="pageIndex != null and pageIndex != ''">
            limit #{(pageIndex-1)*pageSize},#{pageIndex*pageSize}
        </if>
    </select>

limit字句中是不允許運算的,而#{}表示的是一個占位符,所以報錯sql語句放到編譯器歷名也不能執行。

解決方案:

#{}變成${},也就是相當於limit后面的值是定值,sql語句是拼接而成的而不是占位符賦值運算:

<select id="getAll" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM t_menu
        <where>
            <if test="title != null and title != ''">
                and  title like concat('%', #{title}, '%')
            </if>
            <if test="deleted != null and deleted != ''">
                and  deleted =  #{deleted}
            </if>
            <if test="dateStar != null and dateStar != ''">
                and gmt_create &gt;=#{dateStar}
            </if>
            <if test="dateEnd != null and dateEnd != ''">
                and gmt_create &lt;=#{dateEnd}
            </if>
        </where>
        ORDER BY pid ASC
        <if test="pageIndex != null and pageIndex != ''">
            limit ${(pageIndex-1)*pageSize},${pageIndex*pageSize}
        </if>
    </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM