91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mybatis-Plus通用枚舉的使用詳解

發布時間:2020-09-23 15:07:05 來源:腳本之家 閱讀:1230 作者:ME_邱康 欄目:開發技術

解決了繁瑣的配置,讓 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通用枚舉內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

胶南市| 新巴尔虎左旗| 安溪县| 东明县| 祁门县| 望都县| 云南省| 三明市| 澄迈县| 永仁县| 垣曲县| 岳阳县| 昆明市| 汉源县| 墨江| 怀安县| 来安县| 襄城县| 来凤县| 英德市| 嘉祥县| 南城县| 开鲁县| 灯塔市| 呼图壁县| 溆浦县| 乌拉特后旗| 汾阳市| 咸阳市| 文登市| 河间市| 宁津县| 大同县| 桃江县| 新源县| 郑州市| 盐津县| 通榆县| 腾冲县| 且末县| 积石山|