MyBatis-Plus分页查询where后面的参数拼接错误报### The error occurred while setting parameters

MyBatis-Plus分页查询条件参数拼接错误!

### The error may exist in file [D:\SourcrGit\IOT_sxx\iot-parent\iot\target\classes\mapper\business\specification\ProductsSpecMapper.xml]

### The error may involve defaultParameterMap

### The error occurred while setting parameters

### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: Method queryTotal execution error of sql :

SELECT COUNT(1) FROM t_products_spec WHERE 1 = 1

错误原因拷贝了相似的mapper文件,Dao层的方法的参数@Param("req"),而mapper中拷贝的不是req前缀,尽管没有用到,统计的sql暴雷了...

检查dao里的参数与mapper里是否完全对上,特别是拷贝别的mapper时!

总结:拷贝代码最容易产生bug!一定要看变量名称、类型,不细心时可能会拷贝后,操作的是同一个变量!或者抽取起来,用新的函数实现相关的逻辑。

IPage<ProductSpecificationEntity> queryByPage(Page<?> page,@Param("req") QueryProductSpecReq req);

看下mapper中的相关SQL语句(正确的)

<select id="queryByPage" resultMap="ProductSpecificationEntity">
        select
         * 
        from t_products_spec
        <where>
            1=1
            <if test="req.categoryId != null and req.categoryId > 0">
                AND category_id = #{req.categoryId}
            </if>
            <if test="req.state != null">
                AND state = #{req.state}
            </if>
            <if test="req.keyword != null and req.keyword != ''">
                AND INSTR(name,#{req.keyword})
                OR INSTR(creator_name,#{req.keyword})
            </if>
        </where>
</select>

导致错误的SQL语句,拷贝过来时,忘记将queryReq修改为req,与Dao层的param 需要保持一致,where条件中的与表中的字段也得一致!尽管请求时只用了page和size,没有走下面的条件,统计计数的语句报如上错误!

<select id="queryByPage" resultMap="ProductSpecificationEntity">
        select
         * 
        from t_products_spec
        <where>
            1=1
            <if test="req.categoryId != null and req.categoryId > 0">
                AND category_id = #{req.categoryId}
            </if>
            <-- 拷贝过来时,忘记将queryReq修改为req !-->
            <if test="queryReq.state != null">
                AND state = #{req.state}
            </if>
            <if test="req.keyword != null and req.keyword != ''">
                AND INSTR(name,#{req.keyword})
                OR INSTR(creator_name,#{req.keyword})
            </if>
        </where>
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 根据引用\[1\]和引用\[2\]的内容,mybatis-plus分页查询需要配置分页插件才能实现真正的分页。如果没有配置分页插件,total(总条数)和pages(总页数)可能会显示为0。所以,需要添加一个配置类来配置分页插件。另外,根据引用\[3\]的内容,mybatis-plusmybatis分页代码上有一些差异。在mybatis中,需要将当前页需要 - 1再乘以每页条数传入#{page};而在mybatis-plus中,可以直接使用new Page(page, size)来实现分页。如果mybatis-plus分页查询500错误,可能是由于分页配置不正确或者前后端传参有问题。请检查分页配置和前后端传参是否正确。 #### 引用[.reference_title] - *1* *3* [【SpringBoot】MyBatisMyBatis-Plus分页查询问题](https://blog.csdn.net/m0_65563175/article/details/130563322)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [mybatis-plus分页查询详解](https://blog.csdn.net/w1014074794/article/details/125787908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值