MyBatis-Plus CURD(四)

MyBatis-Plus CRUD 示例

脚本初始化

schema-h2.sql

1
2
3
4
5
6
7
8
9
10
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)
);

data-h2.sql

1
2
3
4
5
6
7
8
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');

添加依赖

pom.xml

父模块

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
</dependencies>

子模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>mybatis-plus-study-crud</artifactId>

<parent>
<groupId>com.ray</groupId>
<artifactId>mybatis-plus-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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>
</plugin>
</plugins>
</build>

</project>

实体类

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ray.mybatisplusstudycrud.entity;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author Ray
* @date 2018/9/13 0013
* 用户实体对应表 user
*/
@Data
@Accessors(chain = true)
public class User {

private Long id;
private String name;
private Integer age;
private String email;
}

数据访问层

UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.ray.mybatisplusstudycrud.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ray.mybatisplusstudycrud.entity.User;

/**
* @author Ray
* @date 2018/9/13 0013
* MP 支持零配置 UserMapper.xml
* 这个模块演示内置 CURD ,不需要XML部分
*/
public interface UserMapper extends BaseMapper<User> {
}

MP 配置类

MybatisPlusConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.ray.mybatisplusstudycrud.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

/**
* @author Ray
* @date 2018/9/13 0013
* MP 配置类
*/
@Configuration
@MapperScan(basePackages = "com.ray.mybatisplusstudycrud.mapper")
public class MybatisPlusConfig {
}

配置文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test
username: root
password: root
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql

logging:
level:
com.ray.mybatisplusstudycrud: debug

测试类

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ray.mybatisplusstudycrud;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ray.mybatisplusstudycrud.entity.User;
import com.ray.mybatisplusstudycrud.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
* 内置 CRUD 演示
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusStudyCrudApplicationTests {

@Resource
private UserMapper userMapper;

@Test
public void aInsert() {
User user = new User();
user.setName("小羊");
user.setAge(3);
user.setEmail("abc@mp.com");
Assert.assertTrue(userMapper.insert(user) > 0);
// 获取ID
System.err.println("\n插入成功 ID 为:" + user.getId());
}

@Test
public void bDelete() {
Assert.assertTrue(userMapper.deleteById(3L) > 0);
Assert.assertTrue(userMapper.delete(new QueryWrapper<User>().lambda().eq(User::getName, "Sandy")) > 0);
}

@Test
public void cUpdate() {
Assert.assertTrue(userMapper.updateById(new User().setId(1L).setEmail("ab@c.c")) > 0);
Assert.assertTrue(userMapper.update(new User().setName("mp"), new UpdateWrapper<User>().lambda().set(User::getAge,3).eq(User::getId, 2)) > 0);
}

@Test
public void dSelect() {
Assert.assertEquals("ab@c.c", userMapper.selectById(1L).getEmail());
User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getId, 2));
Assert.assertEquals("mp", user.getName());
}

}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2018-09-13 11:20:02.844 DEBUG 8140 --- [           main] c.r.m.mapper.UserMapper.insert           : ==>  Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? ) 

插入成功 ID 为:1040077619422318594
2018-09-13 11:20:02.864 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.insert : ==> Parameters: 1040077619422318594(Long), 小羊(String), 3(Integer), abc@mp.com(String)
2018-09-13 11:20:02.864 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.insert : <== Updates: 1
2018-09-13 11:20:02.882 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.deleteById : ==> Preparing: DELETE FROM user WHERE id=?
2018-09-13 11:20:02.885 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.deleteById : ==> Parameters: 3(Long)
2018-09-13 11:20:02.887 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.deleteById : <== Updates: 1
2018-09-13 11:20:02.937 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.delete : ==> Preparing: DELETE FROM user WHERE name = ?
2018-09-13 11:20:02.950 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.delete : ==> Parameters: Sandy(String)
2018-09-13 11:20:02.951 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.delete : <== Updates: 1
2018-09-13 11:20:02.961 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.updateById : ==> Preparing: UPDATE user SET email=? WHERE id=?
2018-09-13 11:20:02.962 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.updateById : ==> Parameters: ab@c.c(String), 1(Long)
2018-09-13 11:20:02.977 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.updateById : <== Updates: 1
2018-09-13 11:20:02.986 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.update : ==> Preparing: UPDATE user SET name=?, age=? WHERE id = ?
2018-09-13 11:20:02.987 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.update : ==> Parameters: mp(String), 3(Integer), 2(Integer)
2018-09-13 11:20:02.988 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.update : <== Updates: 1
2018-09-13 11:20:02.996 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectById : ==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
2018-09-13 11:20:03.007 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectById : ==> Parameters: 1(Long)
2018-09-13 11:20:03.048 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectById : <== Total: 1
2018-09-13 11:20:03.056 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectOne : ==> Preparing: SELECT id,name,age,email FROM user WHERE id = ?
2018-09-13 11:20:03.059 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectOne : ==> Parameters: 2(Integer)
2018-09-13 11:20:03.063 DEBUG 8140 --- [ main] c.r.m.mapper.UserMapper.selectOne : <== Total: 1
-------------- 本文结束  感谢您的阅读 --------------
0%