Mybaits-Plus实战 枚举注入(九)

MyBatis-Plus 枚举注入示例

脚本初始化

schema-h2.sql

1
2
3
4
5
6
7
8
9
10
11
12
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 '年龄',
gender INT(2) NULL DEFAULT NULL COMMENT '性别,0:MALE, 1:FEMALE',
grade INT(3) NULL DEFAULT NULL COMMENT '年级',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);

data-h2.sql

1
2
3
4
5
6
DELETE FROM user;

INSERT INTO user (id, name, age, email, grade, gender) VALUES
(2, 'Jack', 3, 'test2@baomidou.com', 1, 0),
(3, 'Tom', 1, 'test3@baomidou.com', 2, 1),
(1, 'Billie', 2, 'test5@baomidou.com', 3, null);

添加依赖

父模块 pom.xml

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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>

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

<name>mybatis-plus-study</name>
<description>Demo project for Spring Boot</description>

<modules>
<module>mybatis-plus-study-quickstart-springmvc</module>
<module>mybatis-plus-study-quickstart-springboot</module>
<module>mybatis-plus-study-generator</module>
<module>mybatis-plus-study-crud</module>
<module>mybatis-plus-study-wrapper</module>
<module>mybatis-plus-study-pagination</module>
<module>mybatis-plus-study-auto-fill-metainfo</module>
<module>mybatis-plus-study-logic-delete</module>
<module>mybatis-plus-study-enum</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatisplus.version>3.0.1</mybatisplus.version>
</properties>

<dependencies>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>

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

<!--H2-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!--Test-->
<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>

子模块 pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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-enum</artifactId>

<description>Demo project for Spring Boot</description>

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

枚举类

AgeEnum.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
package com.ray.mybatisplusstudyenum.enums;

import com.baomidou.mybatisplus.core.enums.IEnum;

/**
* @author Ray
* @date 2018/10/8
* 通用枚举注入演示,注意需要实现 IEnums 也需要扫描枚举包
*/
public enum AgeEnum implements IEnum<Integer> {
ONE(1, "一岁"),
TWO(2, "两岁"),
THREE(3, "三岁");

private int value;
private String desc;

AgeEnum(final int value, final String desc) {
this.value = value;
this.desc = desc;
}

/**
* 注意修改,默认return null
* @return value
*/
@Override
public Integer getValue() {
return value;
}
}

注意:修改getValue()

GenderEnum.java

1
2
3
4
5
6
7
8
9
10
package com.ray.mybatisplusstudyenum.enums;

/**
* @author Ray
* @date 2018/10/8
*/
public enum GenderEnum {
MALE,
FEMALE;
}

GradeEnum.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
package com.ray.mybatisplusstudyenum.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

/**
* @author Ray
* @date 2018/10/8
*/
public enum GradeEnum {

PRIMARY(1, "小学"),
SECONDORY(2, "中学"),
HIGH(3, "高中");

@EnumValue
private final int code;
private final String descp;

GradeEnum(int code, String descp) {
this.code = code;
this.descp = descp;
}

public int getCode() {
return code;
}

public String getDescp() {
return descp;
}
}

实体类

BaseEntity.java

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

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

/**
* @author Ray
* @date 2018/10/8
* 基础父类测试
*/
@Data
@Accessors(chain = true)
public class BaseEntity {

private Long id;

}

User.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
package com.ray.mybatisplusstudyenum.entity;

import com.ray.mybatisplusstudyenum.enums.AgeEnum;
import com.ray.mybatisplusstudyenum.enums.GenderEnum;
import com.ray.mybatisplusstudyenum.enums.GradeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author Ray
* @date 2018/10/8
* 用户实体对应表 user
*/
@Data
@Accessors(chain = true)
public class User extends BaseEntity {

private String name;
private String email;

/**
* IEnum接口的枚举处理
*/
private AgeEnum age;

/**
* 原生枚举: 默认使用枚举值顺序: 0:MALE, 1:FEMALE
*/
private GenderEnum gender;

/**
* 原生枚举(带{@com.baomidou.mybatisplus.annotation.EnumValue}):
* 数据库的值对应该注解对应的属性
*/
private GradeEnum grade;
}

数据访问层

UserMapper.java

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

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

/**
* @author Ray
* @date 2018/10/8
*/
public interface UserMapper extends BaseMapper<User> {
}

MP 配置类

MybatisPlusConfig.java

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

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

/**
* @author Ray
* @date 2018/10/8
*/
@Configuration
@MapperScan(basePackages = "com.ray.mybatisplusstudyenum.mapper")
public class MybatisPlusConfig {
}

配置文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

mybatis-plus:
type-enums-package: com.ray.mybatisplusstudyenum.enums

logging:
level:
com.ray.mybatisplusstudyenum: debug

测试类

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
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.ray.mybatisplusstudyenum;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ray.mybatisplusstudyenum.entity.User;
import com.ray.mybatisplusstudyenum.enums.AgeEnum;
import com.ray.mybatisplusstudyenum.enums.GenderEnum;
import com.ray.mybatisplusstudyenum.enums.GradeEnum;
import com.ray.mybatisplusstudyenum.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;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusStudyEnumApplicationTests {

@Resource
private UserMapper userMapper;

@Test
public void insert() {
User user = new User();
user.setName("Ray");
user.setAge(AgeEnum.ONE);
user.setGrade(GradeEnum.HIGH);
user.setGender(GenderEnum.MALE);
user.setEmail("abc@mp.com");
Assert.assertTrue(userMapper.insert(user) > 0);
// 成功直接拿 ID
System.err.println("\n插入成功 ID 为: " + user.getId());

List<User> list = userMapper.selectList(null);
for (User u :
list) {
System.out.println(u);
Assert.assertNotNull("age should not be null", u.getAge());
if (u.getId().equals(user.getId())) {
Assert.assertNotNull("gender should not be null", u.getGender());
Assert.assertNotNull("grade should not be null", u.getGrade());
}
}
}

@Test
public void delete() {
Assert.assertTrue(userMapper.delete(new QueryWrapper<User>()
.lambda().eq(User::getAge, AgeEnum.TWO)) > 0);
}

@Test
public void update() {
Assert.assertTrue(userMapper.update(new User().setAge(AgeEnum.TWO),
new QueryWrapper<User>().eq("age", AgeEnum.THREE)) > 0);
}

@Test
public void select() {
User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getId, 2));
Assert.assertEquals("Jack", user.getName());
Assert.assertTrue(AgeEnum.THREE == user.getAge());
}

}

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2018-10-08 20:53:37.947 DEBUG 10820 --- [           main] c.r.m.mapper.UserMapper.delete           : ==>  Preparing: DELETE FROM user WHERE age = ? 
2018-10-08 20:53:37.999 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.delete : ==> Parameters: 2(Integer)
2018-10-08 20:53:38.002 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.delete : <== Updates: 1
2018-10-08 20:53:38.409 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.insert : ==> Preparing: INSERT INTO user ( id, name, email, age, gender, grade ) VALUES ( ?, ?, ?, ?, ?, ? )
2018-10-08 20:53:38.410 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.insert : ==> Parameters: 1049281665438969857(Long), Ray(String), abc@mp.com(String), 1(Integer), 0(Integer), 3(Integer)
2018-10-08 20:53:38.410 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.insert : <== Updates: 1
2018-10-08 20:53:38.413 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectList : ==> Preparing: SELECT id,name,email,age,gender,grade FROM user

插入成功 ID 为: 1049281665438969857
2018-10-08 20:53:38.418 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectList : ==> Parameters:
2018-10-08 20:53:38.443 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectList : <== Total: 3
User(name=Jack, email=test2@baomidou.com, age=THREE, gender=MALE, grade=PRIMARY)
User(name=Tom, email=test3@baomidou.com, age=ONE, gender=FEMALE, grade=SECONDORY)
User(name=Ray, email=abc@mp.com, age=ONE, gender=MALE, grade=HIGH)
2018-10-08 20:53:38.462 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectOne : ==> Preparing: SELECT id,name,email,age,gender,grade FROM user WHERE id = ?
2018-10-08 20:53:38.462 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectOne : ==> Parameters: 2(Integer)
2018-10-08 20:53:38.463 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.selectOne : <== Total: 1
2018-10-08 20:53:38.478 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.update : ==> Preparing: UPDATE user SET age=? WHERE age = ?
2018-10-08 20:53:38.480 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.update : ==> Parameters: 2(Integer), 3(Integer)
2018-10-08 20:53:38.481 DEBUG 10820 --- [ main] c.r.m.mapper.UserMapper.update : <== Updates: 1
-------------- 本文结束  感谢您的阅读 --------------
0%