SpringBoot升级Mybatis为MybatisPlus遇到的问题及解决

前言

​ MP的安装使用正如他官网说的异常简单,但是在原有的Mybatis项目兼容Mybatis-Plus还是遇到了些许问题:MyBatisPlus的整合(springboot)和使用

先说结论

  1. ​ 检测依赖是否正确引入;

  2. ​ 检测使用的BaseMapper是否正确的依赖;

  3. ​ 检查POJO对应表名称,字段名称是否对应,若表存在主键,需要指定POJO中对应主键(用注解 @TableId 注解表 ID 主键);

  4. ​ 检查是否被扫描到Dao:@MapperScan;

  5. ​ application.properties配置是否恰当;

最终我的问题是:mybatis.mapper-locationsmybatis-plus.mapper-locations导致的。这里我只记录这个过程,1、2、3、4点已在基础博客中有写到:


最初尝试运行

#mybatis相关配置文件
mybatis.mapper-locations=classpath*:mapper/*Dao.xml
#mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml

出现了问题,进行了错误的尝试。


错误的尝试

刚开始测试用的是baseMapper里面的selectList方法,未用到自行编写的映射mapper.xml文件;误以为需要将 mybatis 的 sqlSessionFactoryBean给替换:

添加appliication.properties配置:

mybatis-plus.global-config.sql-session-factory=com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean

报错:

在这里插入图片描述

在这里插入图片描述

终归原因还是配置赋值是String,而要求是SqlSessonFactory。


正确的解决方法

分别给mybatis和mp指定映射文件对象:

值得注意的是如果mybatismybatis-plus两个都写了就要保持一致,如下,如果一个写了,另外一个没写,则会报对应的配置找不到:

#mybatis相关配置文件
mybatis.mapper-locations=classpath*:mapper/*Dao.xml
mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml

单独只配置mybatis,则无法支持mp:

报错:

Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘xx’ \target\classes\mapper\config\sqlMapConfig.xml]’. Cause: java.lang.NullPointerException’


单独将mybatis.mapper-locationsmybatis.config-location替换为对应的mp,则无法兼容原有的mybatis代码;

在这里插入图片描述

最后添加:

#mybatis相关配置文件
mybatis.mapper-locations=classpath*:mapper/*Dao.xml
mybatis.config-location=classpath:mapper/config/sqlMapConfig.xml
mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml
mybatis-plus.config-location=classpath:mapper/config/sqlMapConfig.xml
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot整合MyBatis(Mybatis-Plus)和Swagger的步骤如下: 1. 首先,确保已经创建了一个Spring Boot项目。可以使用Spring Initializr或者手动创建。 2. 在项目的pom.xml文件中,添加MyBatisMybatis-Plus的依赖。例如: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- Mybatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- 其他依赖... --> </dependencies> ``` 3. 创建一个数据库表,并在application.properties/application.yml文件中配置数据库连接信息。 4. 创建MyBatis的Mapper接口和对应的XML文件,定义SQL语句和对应的映射关系。 5. 创建一个实体类,用于映射数据库表的字段。 6. 创建一个Service类,使用@Serive注解标识,并在类中注入Mapper接口,并实现相应的业务逻辑。 7. 创建一个Controller类,使用@RestController注解标识,并使用@Autowired注入Service类,在方法中实现相应的接口。 8. 在启动类上使用@EnableSwagger2注解开启Swagger的支持。 9. 运行项目,访问Swagger UI页面(http://localhost:8080/swagger-ui.html),可以查看和测试API接口。 以上就是Spring Boot整合MyBatis(Mybatis-Plus)和Swagger的基本步骤。根据实际需求,可以进一步配置和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长臂人猿

客官们众筹请博主喝杯奶茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值