Springboot项目Mybatis升级为Mybatis-Plus的详细步骤
作者:开发罗某人
在许多 Java 项目中,MyBatis 是一个广泛使用的 ORM 框架,然而,随着 MyBatis-Plus 的出现,许多开发者开始迁移到这个更加简洁、高效的工具,它在 MyBatis 的基础上提供了更多的功能,所以本文将介绍Springboot项目Mybatis升级为Mybatis-Plus的详细步骤
前言
因项目太老或遗留问题,项目使用mybatis开发效率较低,这里记录下把项目升级为mybatis-plus,提高开发效率。为什么选择只升级到mybatis-plus3.4.0,因为ai说这是与mybatis3.5.10版本最兼容稳定。
项目版本
mybatis版本3.5.10升级到mybatis-plus3.4.0
步骤一
xml添加依赖mybatis-plus3.4.0 建议:可以根据mybatis开发表生成得xml在哪个下就在哪个项目pom.xml加
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>步骤二
搜索项目MyBatisConfig类 或按搜索@MapperScan注解所在类注释掉
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* MyBatis相关配置
* Created by macro on 2019/4/8.
*/
@Configuration
@EnableTransactionManagement
//@MapperScan({"com.macro.mall.mapper"})
public class MyBatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 配置最大页数,防止恶意请求分页查询
paginationInterceptor.setLimit(1000);
// 配置请求分页时,如果页码超过最大页数,自动回到首页
paginationInterceptor.setOverflow(true);
return paginationInterceptor;
}
}步骤三
yml配置文件注释mybatis配置添加mybatis-plus配置 标红地方按自己项目结构路径填写
mapper-locations: classpath:com/*/*/mapper/*.xml 为xml路径
type-aliases-package: com.*.*.model 为bean对象路径
#mybatis:
# mapper-locations:
# - classpath:dao/*.xml
# - classpath*:com/**/mapper/*.xml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # SQL 打印
global-config:
db-config:
id-type: auto # 主键策略
mapper-locations: classpath:com/*/*/mapper/*.xml
type-aliases-package: com.*.*.model解释
mapper-locations含义图解

type-aliases-package 含义图解

步骤四
Application启动类注解配置标红点按自己项目路径配置
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
@MapperScan("com.*.*.mapper")
@ComponentScan(basePackages = "com.*.*")
@SpringBootApplication()
@EnableAsync
public class FriendsApplication {
public static void main(String[] args) {
SpringApplication.run(MakingFriendsApplication.class, args);
}
}解释
@MapperScan含义图解

@ComponentScan含义图解

以上就是Springboot项目Mybatis升级为Mybatis-Plus的详细步骤的详细内容,更多关于Springboot Mybatis升级为Mybatis-Plus的资料请关注脚本之家其它相关文章!
