您好,登錄后才能下訂單哦!
解決了繁瑣的配置,讓 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通用枚舉內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。