mybatis-plus使用中遇到的问题
最近新搭一个测试项目,使用了 mybatis-plus当前的最新版本 3.3.1,之后遇到了一系列问题,特整理一下。
mapper配置问题
报错:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘cn.yc.sys.mapper.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate.
看报错信息,是程序未找到我的mapper,怎么会?这都是使用mybatis-plus自己的生成器生成的代码呀? 再瞄了几眼官网文档,发现如下说明:
汗,赶紧加上,问题解决。
mybatis-plus与mybatis冲突
报错:
Caused by: java.lang.ClassNotFoundException: org.mybatis.logging.LoggerFactory
经排查,我遇到此问题的原因,是pom中同时引用了mybatis-plus与mybatis
本人既然要使用mybatis-plus,自然是去掉mybatis的引用了,之后问题解决。
mybatis-plus与pagehelper冲突
报错:
*************************** APPLICATION FAILED TO START ***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver(MybatisMapperAnnotationBuilder.java:369)
The following method did not exist:
com.baomidou.mybatisplus.core.MybatisConfiguration.getLanguageDriver(Ljava/lang/Class;)Lorg/apache/ibatis/scripting/LanguageDriver;
The method’s class, com.baomidou.mybatisplus.core.MybatisConfiguration, is available from the following locations:
jar:file:/E:/tools_study/apache/maven/localRepository/repository/com/baomidou/mybatis-plus-core/3.3.1/mybatis-plus-core-3.3.1.jar!/com/baomidou/mybatisplus/core/MybatisConfiguration.class
It was loaded from the following location:
file:/E:/tools_study/apache/maven/localRepository/repository/com/baomidou/mybatis-plus-core/3.3.1/mybatis-plus-core-3.3.1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.baomidou.mybatisplus.core.MybatisConfiguration
以上错误,出现的原因可能比较多:
-
mybatis-plus与spring boot版本不一致,这个版本对应可到查看,打开网站,搜索mybatis-plus-boot-starter
不过,问题解决后经过验证,我使用mybatis-plus 3.3.1与spring boot 2.2.1,也能成功(当然,只是启动成功,也许使用中还是会有其他问题,因此版本最好还是对应起来使用)。
-
项目中引用了多个版本的mybatis-plus mybatis-plus与其他包(本人就是此原因)
经过排查,最终发现是项目中引用的pagehelper-spring-boot-starter造成的。
解决方法,两种: 一、升级pagehelper-spring-boot-starter版本,我原本使用的是1.0.0版,升级到1.2.13,问题解决。 二、在pagehelper-spring-boot-starter依赖中添加排除mybatis的选项,如下:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency>
本人直接两种方法结合,升级版本的同时,也添加了排除选项:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency>
至此,问题解决。
PS: mybatis-plus中也自带了分页功能,因此,对于pagehelper没有特殊爱好的话,也可以直接将它去掉,直接使用mybatis-plus分页功能。