文章首推
今日主题:mybatis-plus
简介
mybatis-plus
是mybatis
的增强款,封装很多常见的CRUD
等其他方法,非常好用,我知道他的原因是因为公司后端持久层就是杂他的基础上进行封装的,所以我就学习了一下,还是非常不错的,分多期来写吧
环境
- IDEA
- springboot
- maven3
- mysql5.7
开始
1.首先需要准备一张表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)
);
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.创建一个springboot
初始化工程
根据需要选择
完整
pom.xml
:(大家可以根据需要添加坐标)
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.king</groupId>
<artifactId>mybatis_plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_plus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.在application.yml
中配置mysql
数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
4.编写表对应的实体类User
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long Id;
private String name;
private Integer age;
private String email;
}
5.编写操作实体类的 Mapper
类,直接继承 BaseMapper
,这是 mybatis-plus
封装好的类。
6.先得在启动类里扫描
Mapper
类,即添加 @MapperScan
注解
@SpringBootApplication
//mapper类所在的目录路径
@MapperScan("com.king.mybatis_plus.mapper")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
7.编写测试类
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired(required = false)
private UserMapper userMapper;
/**
* 查询
*/
@Test
public void testSelect(){
/*查询所有的数据UserMapper
中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,
所以不填写就是无任何条件*/
List<User> list=userMapper.selectList(null);
for (User user:list){
System.out.println(user);
}
//根据主键查询
System.out.println("记录:");
System.out.println(userMapper.selectById(1));
}
/**
* 添加
*/
@Test
public void testInsert(){
//主键会自动生成
User user=new User();
user.setName("king");
user.setAge(20);
user.setEmail("123456@qq.com");
System.out.println("插入结果:"+userMapper.insert(user));
}
/**
* 删除
*/
@Test
public void testDelete(){
System.out.println("删除结果:"+userMapper.deleteById(1));
}
/**
* 修改
*/
@Test
public void testUpdate(){
User user=new User();
user.setId(2L);
user.setName("ting");
user.setAge(30);
user.setEmail("2345@qq.com");
System.out.println("修改结果:"+userMapper.updateById(user));
}
}
好的,到这里简单的CRUD就好了,咋们下期再见。
欢迎关注公众号:鹏哥的世界