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-plusmybatis

在这里插入图片描述
在这里插入图片描述
本人既然要使用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-plusspring boot版本不一致,这个版本对应可到https://mvnrepository.com/查看,打开网站,搜索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分页功能。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一个MyBatis的增强版工具,它在MyBatis的基础上扩展了其他功能,但没有改变其基本功能。它的存在是为了简化开发,提高效率。 要使用MyBatis-Plus,可以采用SpringBoot快速使用的方式。具体步骤如下: 1. 首先,在你的项目引入MyBatis-Plus的依赖。可以在项目的pom.xml文件添加MyBatis-Plus的依赖配置。 2. 然后,配置数据库连接信息,可以在项目的配置文件(比如application.properties或application.yml)配置数据库连接信息,包括数据库驱动、url、用户名和密码等。 3. 接着,创建实体类和Mapper接口。实体类用于映射数据库表结构,Mapper接口用于定义数据库操作的方法。 4. 使用MyBatis-Plus提供的注解或方法,进行数据库的增删改查操作。比如可以使用@TableName注解指定实体类对应的数据库表名,使用@Insert注解进行插入操作,使用@Update注解进行更新操作,使用@Delete注解进行删除操作,使用@Select注解进行查询操作等。 5. 最后,在需要使用数据库操作的地方,注入Mapper接口,并调用方法进行相应的操作。 通过以上步骤,就可以使用MyBatis-Plus进行数据库操作了。这样可以简化开发过程,提高效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis-plus使用](https://blog.csdn.net/yr6123/article/details/125890028)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis-Plus 笔记](https://blog.csdn.net/i_silence/article/details/106807077)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值