Mybatis枚舉類(lèi)型轉(zhuǎn)換源碼分析
Mybatis枚舉類(lèi)型轉(zhuǎn)換
類(lèi)型轉(zhuǎn)換器源碼分析
在Mybatis的TypeHandlerRegistry中,添加了常用的類(lèi)轉(zhuǎn)換器,其中默認(rèn)的枚舉類(lèi)型轉(zhuǎn)換器是EnumTypeHandler。
public final class TypeHandlerRegistry { .... public TypeHandlerRegistry(Configuration configuration) { this.unknownTypeHandler = new UnknownTypeHandler(configuration); register(Boolean.class, new BooleanTypeHandler()); register(boolean.class, new BooleanTypeHandler()); register(JdbcType.BOOLEAN, new BooleanTypeHandler()); register(JdbcType.BIT, new BooleanTypeHandler()); ...
EnumTypeHandler.java,默認(rèn)使用的是枚舉的名稱(chēng)設(shè)置參數(shù)和轉(zhuǎn)換枚舉類(lèi)型。
public EnumTypeHandler(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 { if (jdbcType == null) { ps.setString(i, parameter.name()); } else { ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589 } } @Override public E getNullableResult(ResultSet rs, String columnName) throws SQLException { String s = rs.getString(columnName); return s == null ? null : Enum.valueOf(type, s); ...
以下代碼展示了如何為確定枚舉類(lèi)型的類(lèi)型轉(zhuǎn)換器。
- 首先直接獲取對(duì)應(yīng)類(lèi)型的類(lèi)型型轉(zhuǎn)換器,包括原始類(lèi)型,包括raw type(原始類(lèi)型,對(duì)應(yīng)Class),parameterized types(參數(shù)化類(lèi)型), array types(數(shù)組類(lèi)型),這是最精確的匹配。
- 如果是Class類(lèi)型且枚舉類(lèi)型是其接口或父類(lèi),如果是匿名類(lèi),尋找其父類(lèi)的類(lèi)型轉(zhuǎn)換器,否則再不斷遞歸尋找該枚舉類(lèi)的接口類(lèi)型轉(zhuǎn)換器,如果沒(méi)有找到,直接利用反射獲defaultEnumHandler的的對(duì)象,專(zhuān)門(mén)用于處理該枚舉類(lèi)型,在圖中是GroupStatusEnum。
- 如果不是枚舉類(lèi)型,則再?lài)L試其父類(lèi)。
getInstance方法如下
public <T> TypeHandler<T> getInstance(Class<?> javaTypeClass, Class<?> typeHandlerClass) { if (javaTypeClass != null) { try { Constructor<?> c = typeHandlerClass.getConstructor(Class.class); return (TypeHandler<T>) c.newInstance(javaTypeClass); } catch (NoSuchMethodException ignored) { // ignored } catch (Exception e) { throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, e); } } try { Constructor<?> c = typeHandlerClass.getConstructor(); return (TypeHandler<T>) c.newInstance(); } catch (Exception e) { throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e); } }
自定義通用枚舉類(lèi)型
為項(xiàng)目中所有枚舉類(lèi)型定一個(gè)接口
public interface BaseEnum { Integer getValue(); }
枚舉類(lèi)實(shí)現(xiàn)該接口
@AllArgsConstructor @Getter public enum GroupStatusEnum implements BaseEnum { DISBANDED("已解散", 0), NORMAL("正常", 1); private final String desc; private final Integer value; }
定義通用枚舉類(lèi)型轉(zhuǎn)換器
public class GenericEnumHandler<E extends BaseEnum> implements TypeHandler<E> { private final Map<Integer, E> map = new HashMap<>(); public GenericEnumHandler(Class<E> clazz) { E[] constants = clazz.getEnumConstants(); for (E constant : constants) { map.put(constant.getValue(), constant); } } @Override public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i, parameter.getValue()); } @Override public E getResult(ResultSet rs, String columnName) throws SQLException { return map.get(rs.getInt(columnName)); } @Override public E getResult(ResultSet rs, int columnIndex) throws SQLException { return map.get(rs.getInt(columnIndex)); } @Override public E getResult(CallableStatement cs, int columnIndex) throws SQLException { return map.get(cs.getInt(columnIndex)); } }
按照上述源碼分析流程,當(dāng)GroupStatusEnum第一次需要轉(zhuǎn)化數(shù)據(jù)庫(kù)的int時(shí),mybatis去尋找類(lèi)型轉(zhuǎn)換器。
- 我們沒(méi)有為這種類(lèi)型定義專(zhuān)門(mén)的類(lèi)型轉(zhuǎn)換器(TypeHandler<GroupStatusEnum>)。
- 該類(lèi)不是內(nèi)部類(lèi)。
- 該類(lèi)實(shí)現(xiàn)了BaseEnum接口,但是我們沒(méi)有為BaseEnum定義類(lèi)型轉(zhuǎn)換器。
- 使用該類(lèi)和默認(rèn)的枚舉類(lèi)利用反射構(gòu)造一個(gè)對(duì)象處理該枚舉,即new GenericEnumTypeHandler(GroupStatusEnum.class)。
使用方法
mybatis: configuration: local-cache-scope: statement jdbc-type-for-null: null use-generated-keys: true cache-enabled: false map-underscore-to-camel-case: true default-enum-type-handler: com.windcf.easychatjava.typehandler.GenericEnumHandler mapper-locations: classpath:/mappers/**/*.xml # type-handlers-package: com.windcf.easychatjava.typehandler
到此這篇關(guān)于Mybatis枚舉類(lèi)型轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Mybatis枚舉類(lèi)型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot中@NotNull,@NotBlank注解使用
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中集成Validation與@NotNull,@NotBlank等注解的簡(jiǎn)單使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08解決@PathVariable對(duì)于特殊字符截?cái)嗟膯?wèn)題
這篇文章主要介紹了解決@PathVariable對(duì)于特殊字符截?cái)嗟膯?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06IDEA在Maven項(xiàng)目中使用本地jar包的方法
我們?cè)谀玫脚f項(xiàng)目的時(shí)候,經(jīng)常會(huì)遇到一種情況,就是這個(gè)項(xiàng)目的maven中依賴(lài)了一個(gè)本地的jar包,這種情況就需要引入這個(gè)jar包,所以本文給大家介紹了IDEA在Maven項(xiàng)目中使用本地jar包的方法,需要的朋友可以參考下2024-04-04Spring?Boot自動(dòng)配置的原理及@Conditional條件注解
這篇文章主要介紹了Spring?Boot自動(dòng)配置的原理及@Conditional條件注解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-07-07Java 中責(zé)任鏈模式實(shí)現(xiàn)的三種方式
本文重點(diǎn)給大家介紹java中如何編寫(xiě)責(zé)任鏈模式。主要從下面3個(gè)框架中的代碼中介紹。非常不錯(cuò),需要的朋友參考下吧2017-09-09SpringCloud的@RefreshScope 注解你了解嗎
這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09