项目场景:
项目中使用了mybatis-plus,本意简化mapper书写内容,结果BaseMapper中只有insert可以使用,其余功能方法不可用。
引用jar包maven
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.2</version>
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</exclusion>
</exclusions>
</dependency>
问题描述
整合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.<init>(MapperMethod.java:235)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(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:要细心啊