欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot mybatis枚舉映射示例代碼

 更新時(shí)間:2019年09月06日 09:23:26   作者:張占嶺  
這篇文章主要給大家介紹了關(guān)于spring boot mybatis枚舉映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

在mybatis和mybatis plus里,如果你的實(shí)體字段是一個(gè)枚舉類型,而在數(shù)據(jù)表里是整型,這時(shí)在存儲(chǔ)時(shí)需要進(jìn)行處理,默認(rèn)情況下,會(huì)把枚舉的元素名稱拼接到SQL語句里,而由于數(shù)據(jù)表是int類型,所以在插入等操作時(shí),就會(huì)出現(xiàn)異常!

添加枚舉處理器

MappedTypes(value = {YesOrNo.class})
public class UniversalEnumHandler<E extends Enum<E> & BaseEnum> extends BaseTypeHandler<E> {

 private final Class<E> type;

 /**
 * construct with parameter.
 */
 public UniversalEnumHandler(Class<E> type) {
 if (type == null) {
  throw new IllegalArgumentException("Type argument cannot be null");
 }
 this.type = type;
 }

 @Override
 public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
  throws SQLException {
 ps.setInt(i, parameter.getCode());
 }

 @Override
 public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
 int code = rs.getInt(columnName);
 return rs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
 }

 @Override
 public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
 int code = rs.getInt(columnIndex);
 return rs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
 }

 @Override
 public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
 int code = cs.getInt(columnIndex);
 return cs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
 }
}

在配置文件指定處理器

mybatis-plus:
 typeHandlersPackage: cn.pilipa.account.cerebrum.client.enums #處理器所在包,我是把枚舉處理器放在枚舉包里

定義代表枚舉鍵值的接口

public interface BaseEnum<E extends Enum<?>, T> {

 public Integer getCode();

 public String getText();
}

定義一下枚舉

public enum YesOrNo implements BaseEnum {
 Yes(1, "是"),
 No(0, "否");
 private Integer code;
 private String text;

 YesOrNo(Integer code, String text) {
 this.code = code;
 this.text = text;
 }

 @JsonCreator
 public static YesOrNo jsonCreate(Integer code) {
 return EnumUtils.codeOf(YesOrNo.class, code);
 }

 @Override
 public Integer getCode() {
 return this.code;
 }

 @Override
 public String getText() {
 return this.text;
 }

 @JsonValue
 public Integer getCodeStr() {
 return this.code;
 }

}

在實(shí)體中定義枚舉類型字段

 /**
 * 是否為國民.
 */
 private YesOrNo naturalBorn;

生成的SQL語句

 ==> Preparing: INSERT INTO employee_info ( id, name, credit_number, status, first_time, tax_code, natural_born ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) 
2019-09-05 16:56:38.991 DEBUG [accounting-client,,,] 92833 --- [   main] c.p.a.c.c.m.EmployeeInfoMapper.insert : 
==> Parameters: 1169534796253630466(Long), 段會(huì)濤(String), 130523199011111219(String), 1(Integer), 2019-09-05(Date), 130523199011111219(String), 0(Integer)

從上面結(jié)果中看到,我們的natural_born對(duì)應(yīng)的值已經(jīng)是int類型了,表示處理器成功了!

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論