BUG mybatis-plus的公用方法not found,自定义方法可以正常调用

项目场景:

项目中使用了mybatis-plus,本意简化mapper书写内容,结果BaseMapper中只有insert可以使用,其余功能方法不可用。

引用jar包maven

		
            com.baomidou
            mybatis-plus-boot-starter
            3.5.2
        
        
            com.baomidou
            mybatis-plus
            3.5.2
            
                
                    com.baomidou
                    mybatis-plus-generator
                
            
        

问题描述

整合mybatis-plus之后,进行test时候发现insert可以正常插入数据,但是其余的公共方法都不行,Invalid bound statement (not found)
报错如下:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.lmsy.mall.modules.product.dao.BrandDao.selectById

	at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:235)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.(MybatisMapperMethod.java:50)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:115)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
	at com.sun.proxy.$Proxy103.selectById(Unknown Source)

原因分析:

按照网上描述的方案,都是复制粘贴,没有一个可以解决我的问题的,什么映射不对了,方法名不一致了,拼写错误了,SqlSessionFactoryBean 换成MybatisSqlSessionFactoryBean 了,全都没有用


一看网上都是复制粘贴的解决方案,决定不看了,还是自己排查吧,功夫不负有心人,一些日志引起了我的注意:

2022-10-03 12:48:38.917  WARN 17496 --- [           main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.lmsy.mall.modules.product.entity.AttrEntity".
2022-10-03 12:48:38.917  WARN 17496 --- [           main] c.b.m.core.injector.DefaultSqlInjector   : class com.lmsy.mall.modules.product.entity.AttrEntity ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.
2022-10-03 12:48:38.942  WARN 17496 --- [           main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.lmsy.mall.modules.product.entity.AttrGroupEntity".
2022-10-03 12:48:38.943  WARN 17496 --- [           main] c.b.m.core.injector.DefaultSqlInjector   : class com.lmsy.mall.modules.product.entity.AttrGroupEntity ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.
2022-10-03 12:48:38.967  WARN 17496 --- [           main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.lmsy.mall.modules.product.entity.BrandEntity".
2022-10-03 12:48:38.967  WARN 17496 --- [           main] c.b.m.core.injector.DefaultSqlInjector   : class com.lmsy.mall.modules.product.entity.BrandEntity ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.
2022-10-03 12:48:39.021  WARN 17496 --- [           main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.lmsy.mall.modules.product.entity.CategoryEntity".
2022-10-03 12:48:39.021  WARN 17496 --- [           main] c.b.m.core.injector.DefaultSqlInjector   : class com.lmsy.mall.modules.product.entity.CategoryEntity ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.
2022-10-03 12:48:39.140  WARN 17496 --- [           main] c.b.m.core.metadata.TableInfoHelper      : Can not find table primary key in Class: "com.lmsy.mall.modules.product.entity.SkuInfoEntity".
2022-10-03 12:48:39.141  WARN 17496 --- [           main] c.b.m.core.injector.DefaultSqlInjector   : class com.lmsy.mall.modules.product.entity.SkuInfoEntity ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method.

Can not find table primary key in Class

在这个class中没有找到主键????

Not found @TableId annotation, Cannot use Mybatis-Plus ‘xxById’ Method.

找不到@TableId 注解,你不能用Mybatis-Plus提供的xxById类型的方法


解决方案:

日志给的提示很明显了,于是我在Entity中加入了@TableId注解
import com.baomidou.mybatisplus.annotation.TableId;
结果真的可以了,虽然问题解决了,但是没有开心的感觉

总结

不要一味地去网上找答案,现在网上很多答案都是千篇一律,给你的始终只是大概的方向,不要因为这个浪费自己时间 ,自己还是需要认真的检查代码,耐心地查看日志,日志的存在就是为了帮助我们排查问题的。
PS:要细心啊

展开阅读全文

页面更新:2024-03-12

标签:法名   方法   功夫不负有心人   注解   能用   解决方案   答案   项目   网上   日志

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top