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>