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

发布时间:2020-09-23 15:07:05 作者:ME_邱康
来源:脚本之家 阅读:1230

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

修改表结构

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

定义枚举

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;
 }

}

配置

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

修改实体

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

测试

插入

@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

[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通用枚举的使用详解

查询

@Test
public void testSelectById() {
 User user = new User();
 user.setId(11L);

 User user1 = user.selectById();
 System.out.println(user1);
}

SQL

[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=女)

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

条件查询

@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

[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通用枚举内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

推荐阅读:
  1. 详解c# 枚举
  2. 详解JAVA枚举

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis-plus 通用枚举

上一篇:ionic2中使用自动生成器

下一篇:如何使用vue-cli来搭建vue项目和webpack

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》