服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Mybatis-Plus通用枚举的使用详解

Mybatis-Plus通用枚举的使用详解

2021-09-24 00:45ME_邱康 Java教程

这篇文章主要介绍了Mybatis-Plus通用枚举的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

解决了繁琐的配置,让 mybatis 优雅的使用枚举属性!

修改表结构

?
1
ALTER TABLE `tb_user` ADD COLUMN `sex` INT ( 1 ) NULL DEFAULT 1 COMMENT '1-男,2-女' AFTER `deleted`;

定义枚举

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public enum SexEnum implements IEnum<Integer> {
 
 MAN(1, "男"), WOMAN(2, "女");
 
 private int value;
 private String desc;
 
 SexEnum(int value, String desc) {
 this.value = value;
 this.desc = desc;
 }
 
 @Override
 public Integer getValue() {
 return this.value;
 }
 
 @Override
 public String toString() {
 return this.desc;
 }
 
}

配置

?
1
2
# 枚举包扫描
mybatis-plus.type-enums-package=cn.com.javakf.enums

修改实体

?
1
private SexEnum sex; // 性别,枚举类型

测试

插入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test
public void testInsert() {
 User user = new User();
 user.setUserName("diaochan");
 user.setPassword("123456");
 user.setAge(20);
 user.setName("貂蝉");
 user.setMail("diaochan@163.com");
 user.setVersion(1);
 user.setSex(SexEnum.WOMAN); // 使用的是枚举
 
 // 调用AR的insert方法进行插入数据
 boolean insert = user.insert();
 System.out.println("result => " + insert);
}

SQL

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[main] [cn.com.javakf.mapper.UserMapper.insert]-[DEBUG] ==> Preparing: INSERT INTO tb_user ( user_name, password, name, age, email, version, sex ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
[main] [cn.com.javakf.mapper.UserMapper.insert]-[DEBUG] ==> Parameters: diaochan(String), 123456(String), 貂蝉(String), 20(Integer), diaochan@163.com(String), 1(Integer), 2(Integer)
[main] [cn.com.javakf.mapper.UserMapper.insert]-[DEBUG] <==  Updates: 1
 Time:43 ms - ID:cn.com.javakf.mapper.UserMapper.insert
Execute SQL:
  INSERT
  INTO
    tb_user
    ( user_name, password, name, age, email, version, sex )
  VALUES
    ( 'diaochan', '123456', '貂蝉', 20, 'diaochan@163.com', 1, 2 )
 
[main] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@44550792]
result => true

Mybatis-Plus通用枚举的使用详解

查询

?
1
2
3
4
5
6
7
8
@Test
public void testSelectById() {
 User user = new User();
 user.setId(11L);
 
 User user1 = user.selectById();
 System.out.println(user1);
}

SQL

?
1
2
3
4
5
[main] [cn.com.javakf.mapper.UserMapper.selectById]-[DEBUG] ==> Preparing: SELECT id,user_name,name,age,email AS mail,version,deleted,sex FROM tb_user WHERE id=? AND deleted=0
[main] [cn.com.javakf.mapper.UserMapper.selectById]-[DEBUG] ==> Parameters: 12(Long)
[main] [cn.com.javakf.mapper.UserMapper.selectById]-[DEBUG] <==   Total: 1
[main] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@474c9131]
User(id=12, userName=diaochan, password=null, name=貂蝉, age=20, mail=diaochan@163.com, address=null, version=1, deleted=0, sex=女)

从测试可以看出,可以很方便的使用枚举了。

条件查询

?
1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testSelectBySex() {
 User user = new User();
 
 QueryWrapper<User> wrapper = new QueryWrapper<>();
 wrapper.eq("sex", SexEnum.WOMAN); // 查询性别为女的数据
 
 List<User> users = user.selectList(wrapper);
 for (User user1 : users) {
 System.out.println(user1);
 }
}

SQL

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==> Preparing: SELECT id,user_name,name,age,email AS mail,version,deleted,sex FROM tb_user WHERE deleted=0 AND sex = ?
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] ==> Parameters: 2(Integer)
[main] [cn.com.javakf.mapper.UserMapper.selectList]-[DEBUG] <==   Total: 1
 Time:15 ms - ID:cn.com.javakf.mapper.UserMapper.selectList
Execute SQL:
  SELECT
    id,
    user_name,
    name,
    age,
    email AS mail,
    version,
    deleted,
    sex
  FROM
    tb_user
  WHERE
    deleted=0
    AND sex = 2
 
[main] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3a3b10f4]
User(id=12, userName=diaochan, password=null, name=貂蝉, age=20, mail=diaochan@163.com, address=null, version=1, deleted=0, sex=女)

到此这篇关于Mybatis-Plus通用枚举的使用详解的文章就介绍到这了,更多相关Mybatis-Plus通用枚举内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45730091/article/details/107361513

延伸 · 阅读

精彩推荐