springboot中怎么利用mybatis實(shí)現(xiàn)枚舉映射

springboot中怎么利用mybatis實(shí)現(xiàn)枚舉映射,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供長寧企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、網(wǎng)站設(shè)計、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為長寧眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jì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), 段會濤(String), 130523199011111219(String), 1(Integer), 2019-09-05(Date), 130523199011111219(String), 0(Integer)

看完上述內(nèi)容,你們掌握springboot中怎么利用mybatis實(shí)現(xiàn)枚舉映射的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標(biāo)題:springboot中怎么利用mybatis實(shí)現(xiàn)枚舉映射
網(wǎng)頁URL:http://bm7419.com/article46/iihihg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站策劃品牌網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司