使用SpringBoot + MybatisPlus时
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependency>
<artifactId>mybatis-plus-boot-starter</artifactId>
<groupId>com.baomidou</groupId>
<version>3.4.0</version>
</dependency>
</dependencyManagement>
编译启动无异常,运行时有数据库操作就会跑出如下异常
java.lang.IllegalStateException: LambdaUtils.getColumnMap(this.entityClass) must not be null
at com.baomidou.mybatisplus.extension.kotlin.AbstractKtWrapper.initNeed(AbstractKtWrapper.kt:62) ~[mybatis-plus-extension-3.4.0.jar:3.4.0]
at com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper.<init>(KtQueryWrapper.kt:49) ~[mybatis-plus-extension-3.4.0.jar:3.4.0]
at com.xxx.xxxxx.xxx.xxx.xxx.queryForPage(LessonsCommentRepositoryImpl.kt:21) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_275]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_275]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_275]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_275]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.5.RELEASE.jar:5.2.5.RELEASE]
检查发现项目中未引入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
补充更新
以上方法虽然解决了问题,但不是问题的根本原因
产生问题的原因是
仔细核对了MybatisPlus的的扫描配置,确认是没问题的,然后跟踪DefaultListableBeanFactory中的beanDefinitionMap,
发现相应Dao(BaseMapper的实现类)是有注入到容器的,只是对应的BeanDefinition是BeetlSqlFactoryBean(在Autowired相关Dao的地方也能看到Beetl相关信息),不报异常的项目是MapperFactoryBean。
所以是beetlsql和mybatis冲突了,导致MapperFactoryBean未被加载。
为什么引入mybatis-spring-boot-starter 解决了问题
我们引入的mybatis-spring-boot-starter是2.0.1版本,其引入的是mybaits-spring2.0.1(之前通过mybatisplus3.4.0引入的是mybaits-spring2.0.5),在mybaits-spring2.0.1是没有MapperScannerConfigurer的,跟Beetl一样是直接在ImportBeanDefinitionRegistrar中通过自定义Scanner注册Dao的,那都是ImportBeanDefinitionRegistrar又为啥选择了mybaits而不是beetl呢?
原因是Spring的第一次Bean扫描是从程序启动类上标记的ComponentScan指定的basePackages开始的,而这次扫描会扫描到我们的MybatisConfig配置文件,这时候MybatisConfig上标记的MapperScan就被识别了,所以比Beetl的配置先注入到容器。