SpringBoot+MyBatisPlus
Spring Boot 结合 MyBatis Plus 是一种常见的 Java 后端开发框架组合,能够快速构建高性能、易于维护的 CRUD 应用程序。以下是 Spring Boot 集成 MyBatis Plus 的基本步骤
一、快速体验
注意:下面版本 idea2020 + SpringBoot2.* + MyBatis-Plus3.4之前的版本
1. 创建Maven项目
选择使用阿里云
选择依赖:
生成的pom.xml 关键依赖:
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2. 配置文件
默认生成的效果:
# 应用名称
spring.application.name=springboot-mybatisplus
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=***
spring.datasource.password=***
我们只需修改密码、用户名、数据库就可以了
注意:
现在的版本在使用SpringBoot 初始化工具已经不能选择 mybatis-plus 依赖,也不再生成上面的配置了
自己配置 application.yml:
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/booksys
sql:
init:
data-locations: classpath:db/data.sql # 插入记录的数据库脚本
schema-locations: classpath:db/schema.sql # 建库建表的数据库脚本
mode: embedded # 如果是嵌入式数据库 则执行
# mode: never # 都不执行
# mode: always # 总是执行sql脚本
其中的初始化数据库模式区分三种: embedded 只适用于嵌入式数据库会执行脚本
3. 编写POJO
@Data
public class Book {
private Integer bookid;
private String bookname;
private Float price;
private Date pubdate;
private String author;
}
4. 编写接口继承BaseMapper
/**
* 接口
* 注意:注解 @Mapper
*/
@Mapper
public interface IBookDao extends BaseMapper<Book> {
}
5. 测试
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
private IBookDao bookDao;
@Test
void contextLoads() {
Integer count = bookDao.selectCount(null);
System.out.println(count);
bookDao.selectList(null).forEach(System.out::println);
}
}
6. 版本组合
注意下面两种组合版本:
springboot3 必须要和 Mybatis-Plus3.5以上版本
- idea2022 + springboot2.7.6 + MybatisPlus3.4.2
- idea2022 + springboot3.0.5 + MybatisPlus3.5.3
组合一:idea2022 + springboot2.7.5 + MybatisPlus3.4.2
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wdzl</groupId>
<artifactId>springboot2_MybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2_MybatisPlus</name>
<description>springboot2_MybatisPlus</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
组合二:idea2022 + springboot3.0.5 + MybatisPlus3.5.3
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wdzl</groupId>
<artifactId>springboot3_MybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot3_MybatisPlus</name>
<description>springboot3_MybatisPlus</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>