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>
复制代码

 

posted @   创客未来  阅读(16085)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示