Springboot项目mybatis如何升级为mybatis-plus

后端 潘老师 5个月前 (11-29) 216 ℃ (0) 扫码查看

本文主要讲解Springboot项目mybatis如何升级为mybatis-plus,由于之前的老项目都是使用的mybatis,现在想要升级为mybatis-plus,我们来看一下升级步骤。

第一步:pom依赖

首先将pom依赖更换,删除原先的mybatis-spring-boot-starter删除,然后新增mybatis-plus-boot-starter依赖,我这里是版本依赖更换如下:

<!-- 删除 -->
<dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.2.0</version>
</dependency>

<!-- 新增 -->
<dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.3</version>
</dependency>

第二步:修改yaml配置

删除原先的application.yml中的mybatis配置,新增mybatis-plus配置,我这里的配置修改如下:

# 删除MyBatis配置
mybatis:
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapper-locations: classpath:mapper/*.xml
  # 配置类型别名
  type-aliases-package: com.cpte.box.entity, com.cpte.box.domain
  # 配置全局懒加载
  configuration:
    lazy-loading-enabled: true
    aggressive-lazy-loading: false

#新增mybatis-plus配置
mybatis-plus:
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapper-locations: classpath:mapper/*.xml
  # 配置类型别名
  type-aliases-package: com.cpte.box.entity, com.cpte.box.domain
  #这里省略了其他配置,可以自己根据需要补充...

第三步:启动运行

这时候,我们启动运行项目,发现正常启动基本就没什么问题,如果发现报错如下:

Caused by: java.lang.ClassNotFoundException: org.mybatis.logging.LoggerFactory

可能是你原项目中使用了page-helper分页插件,可能是版本不兼容mybatis-plus导致的,可以自己尝试升级版本并排除部分冲突依赖:

我的原来的分页插件依赖:

<!--分页插件pagehelper依赖-->
<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.5</version>
</dependency>

 修改后,需要排除其中的3个依赖:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
        </exclusion>
    </exclusions>
</dependency>

如果你还想要后期兼容mybatis-plus自带的分页插件,也可以在在confing中新增mybatis-plus分页插件配置类如下:

/**
 * @author panziye
 * @description <p>www.panziye.com</p>
 * @date 2023-11-29 15:44
 **/
@Configuration
@MapperScan("com.cpte.box.mapper")
public class MybatisPlusConfig {

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

经过以上操作,本次Springboot项目mybatis成功升级为mybatis-plus框架了。

以上就是Springboot项目mybatis如何升级为mybatis-plus的全部内容,,希望对你有帮助。欢迎持续关注潘子夜个人博客(www.panziye.com),学习愉快哦!


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/back/11959.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】