Mybatis-Plus通用枚举的使用详解
解决了繁琐的配置,让 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
查询
@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通用枚举内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)