MybatisPlusException:Failed to process,Error SQL异常报错的解决办法
作者:本本本添哥
一、问题描述
Error querying database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL: SELECT
goods_spu.`id`,
goods_spu.`tenant_id`,
goods_spu.`shop_id`,
goods_spu.`spu_code`,
goods_spu.`name`,
goods_spu.`sell_point`,
null `description`,
goods_spu.`category_first`,
goods_spu.`category_second`,
goods_spu.`category_shop_first`,
goods_spu.`category_shop_second`,
goods_spu.`pic_urls`,
goods_spu.`shelf`,
goods_spu.`sort`,
goods_spu.`price_down`,
goods_spu.`price_up`,
goods_spu.`sale_num`,
goods_spu.`create_time`,
goods_spu.`update_time`,
goods_spu.`spec_type`,
goods_spu.`del_flag`,
goods_spu.`points_give_switch`,
goods_spu.`points_give_num`,
goods_spu.`points_deduct_switch`,
goods_spu.`points_deduct_scale`,
goods_spu.`points_deduct_amount`,
goods_spu.`freight_templat_id`,
goods_spu.`verify_status`,
goods_spu.`verify_detail`,
goods_spu.`channel`,
goods_spu.`self_pick_addr_id`
, sum(s.stock) as stock
FROM goods_spu as goods_spu
left join goods_sku as s on goods_spu.id=s.spu_id
group by goods_spu.id;
WHERE goods_spu.`name` LIKE CONCAT('%',?,'%') ### Cause:
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy180.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy195.selectPage1WithStock(Unknown Source)
at com.joolun.cloud.mall.admin.service.impl.GoodsSpuServiceImpl.page1WithStock(GoodsSpuServiceImpl.java:72)
at com.joolun.cloud.mall.admin.service.impl.GoodsSpuServiceImpl$$FastClassBySpringCGLIB$$cb4904f8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.joolun.cloud.mall.admin.service.impl.GoodsSpuServiceImpl$$EnhancerBySpringCGLIB$$936a8c1a.page1WithStock(<generated>)
at com.joolun.cloud.mall.admin.controller.GoodsSpuController.getGoodsSpuPageWithStock(GoodsSpuController.java:82)
at com.joolun.cloud.mall.admin.controller.GoodsSpuController$$FastClassBySpringCGLIB$$4d6ff776.invoke(<generated>)
二、需求描述
运营提出,对首页进行店铺装修时,需看到库存字段,
这样不会上到了0库存的品,导致了用户下单体验的问题。
因此,在Mybatis Plus中新增了库存字段以展示
三、定位错误代码
因为原来这个接口是是做了一个基于Mybatis Plus的分页,但是因为库存这个字段,存在sku的表中
因此使用left join ,但是又因为SPU与SKU之间是一对多的关系,因此会有多个0库存的情况,
这里采用了count进行加和的方式,不得不说,设计的非常巧妙,因为加和起来库存都为0的话,说明肯定是0库存的呀。
因此有了以下代码
<select id="selectPage1WithStock" resultMap="goodsSpuMap5"> SELECT <include refid="goodsSpuSql2"/>, sum(s.stock) as stock FROM goods_spu as goods_spu left join goods_sku as s on goods_spu.id=s.spu_id group by goods_spu.id; <where> <if test="query.shopId != null"> AND goods_spu.`shop_id` = #{query.shopId} </if> <if test="query.shelf != null"> AND goods_spu.`shelf` = #{query.shelf} </if> <if test="query.verifyStatus != null"> AND goods_spu.`verify_status` = #{query.verifyStatus} </if> <if test="query.categorySecond != null"> AND goods_spu.`category_second` = #{query.categorySecond} </if> <if test="query.name != null"> AND goods_spu.`name` LIKE CONCAT('%',#{query.name},'%') </if> <if test="query.spuCode != null"> AND goods_spu.`spu_code` = #{query.spuCode} </if> <if test="query.specType != null"> AND goods_spu.`spec_type` = #{query.specType} </if> <if test="query.pointsGiveSwitch != null"> AND goods_spu.`points_give_switch` = #{query.pointsGiveSwitch} </if> <if test="query.pointsDeductSwitch != null"> AND goods_spu.`points_deduct_switch` = #{query.pointsDeductSwitch} </if> </where> </select>
以下代码看起来,没有问题,但是确有一个问题,注意在,存在一个分号,
我们都知道,分号是一段SQL语句的结尾,所以,
以上代码肯定是后面的语句肯定是执行不到的
但是到底是group by 语句放在前面呢?还是where 语句在前,这个问题,可以直接在数据库验证下即可
四、数据库验证
写法一:WHERE在前,GROUP BY在后
SELECT goods_spu.*, sum(s.stock) AS stock FROM goods_spu AS goods_spu LEFT JOIN goods_sku AS s ON goods_spu.id = s.spu_id WHERE goods_spu.`name` LIKE CONCAT('%', 'A', '%') GROUP BY goods_spu.id
发现,数据能够正常查询
写法二:GROUP BY在前,WHERE在后
会发现无法正常查询出来结果
五、修改Mybatis中的SQL语句
经过以上的验证,得到了最终修改之后的SQL语句如下
<select id="selectPage1WithStock" resultMap="goodsSpuMap5"> SELECT <include refid="goodsSpuSql2"/>, sum(s.stock) as stock FROM goods_spu as goods_spu left join goods_sku as s on goods_spu.id=s.spu_id <where> <if test="query.shopId != null"> AND goods_spu.`shop_id` = #{query.shopId} </if> <if test="query.shelf != null"> AND goods_spu.`shelf` = #{query.shelf} </if> <if test="query.verifyStatus != null"> AND goods_spu.`verify_status` = #{query.verifyStatus} </if> <if test="query.categorySecond != null"> AND goods_spu.`category_second` = #{query.categorySecond} </if> <if test="query.name != null"> AND goods_spu.`name` LIKE CONCAT('%',#{query.name},'%') </if> <if test="query.spuCode != null"> AND goods_spu.`spu_code` = #{query.spuCode} </if> <if test="query.specType != null"> AND goods_spu.`spec_type` = #{query.specType} </if> <if test="query.pointsGiveSwitch != null"> AND goods_spu.`points_give_switch` = #{query.pointsGiveSwitch} </if> <if test="query.pointsDeductSwitch != null"> AND goods_spu.`points_deduct_switch` = #{query.pointsDeductSwitch} </if> </where> group by goods_spu.id </select>
六、修复后的业务验证
经过测试验证,分页也能正常,而且模糊查询也顺利。
七、分析错误原因
出现该问题的原因有两个
(1)对Mybatis的 where标签 和 if 标签不熟悉
where标签的三个特性:
where标签可以被解析为where 关键字只有if标签有内容的情况下才会插入where子句;第二,若子句的开通为 “AND” 或 “OR”,where标签会将它替换去除; (2)开发完成代码后,应该做回归测试
回归测试是指修改了旧代码后,重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误。
总结
到此这篇关于MybatisPlusException:Failed to process,Error SQL异常报错解决的文章就介绍到这了,更多相关MybatisPlusException:Failed to process,Error SQL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!