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