DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
往User表中插入数据,其Data脚本如下:
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
步骤2:利用Spring Initializer快速初始化一个Spring Boot工程,并引入相关依赖:
<?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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.misu</groupId>
<artifactId>springboot-mybatis-plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-plus</name>
<description>SpringBoot整合MyBatis-Plus</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
步骤3:在application.properties添加如下配置:
#MySQL数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
步骤4:在启动类中添加@MapperScan注解,扫描Mapper文件夹:
package com.misu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.misu.mapper")
public class SpringbootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
}
步骤5:在IDEA中安装MyBatisX插件的前提下,可以自动生成相应的domain、mapper、service、serviceImpl、mapper.xml 在IDEA的Database(前提是已连接相应的数据库)中,选中user表并右键,选择MybatisX-Generator Step1:
Step2: Step3: Step4:
步骤6:测试类
package com.misu;
import com.misu.domain.User;
import com.misu.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
@Resource
UserMapper userMapper;
@Test
void contextLoads() {
}
@Test
public void add() {
User user = new User();
user.setId(6L);
user.setName("Tiramisu");
user.setAge(18);
user.setEmail("tiramisu@baomidou.com");
userMapper.insert(user);
}
@Test
public void selectAll() {
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void selectById() {
User user = userMapper.selectById(6L);
System.out.println(user);
}
@Test
public void updateById() {
User user = new User();
user.setId(6L);
user.setAge(26);
userMapper.updateById(user);
}
@Test
public void deleteById() {
userMapper.deleteById(6L);
}
}
运行结果:
其他的几个方法的运行结果这里就不一一 展示了 Tips: 1.通过以上几个简单的步骤,我们就实现了User表的CRUD功能,甚至连XML文件都不用编写! 2.从以上步骤中,我们可以看到集成MyBatis-Plus非常的简单,只需要引入 starter工程,并配置mapper扫描路径即可。 3.当然MyBatis-Plus的强大远远不止这些功能,有兴趣的同学可以到官网学习:https://mp.baomidou.com/