上篇文章《深刻淺出Mybatis系列(四)---配置詳解之typeAliases別名(mybatis源碼篇)》爲你們介紹了mybatis中別名的使用,以及其源碼。本篇將爲你們介紹TypeHandler, 並簡單分析其源碼。html
Mybatis中的TypeHandler是什麼?java
不管是 MyBatis 在預處理語句(PreparedStatement)中設置一個參數時,仍是從結果集中取出一個值時,都會用類型處理器將獲取的值以合適的方式轉換成 Java 類型。Mybatis默認爲咱們實現了許多TypeHandler, 當咱們沒有配置指定TypeHandler時,Mybatis會根據參數或者返回結果的不一樣,默認爲咱們選擇合適的TypeHandler處理。sql
那麼,Mybatis爲咱們實現了哪些TypeHandler呢? 咱們怎麼自定義實現一個TypeHandler ? 這些都會在接下來的mybatis的源碼中看到。數據庫
在看源碼以前,仍是像以前同樣,先看看怎麼配置吧?mybatis
配置TypeHandler:app
<configuration> <typeHandlers> <!-- 當配置package的時候,mybatis會去配置的package掃描TypeHandler <package name="com.dy.demo"/> --> <!-- handler屬性直接配置咱們要指定的TypeHandler --> <typeHandler handler=""/> <!-- javaType 配置java類型,例如String, 若是配上javaType, 那麼指定的typeHandler就只做用於指定的類型 --> <typeHandler javaType="" handler=""/> <!-- jdbcType 配置數據庫基本數據類型,例如varchar, 若是配上jdbcType, 那麼指定的typeHandler就只做用於指定的類型 --> <typeHandler jdbcType="" handler=""/> <!-- 也可二者都配置 --> <typeHandler javaType="" jdbcType="" handler=""/> </typeHandlers> ...... </configuration>
上面簡單介紹了一下TypeHandler, 下面就看看mybatis中TypeHandler的源碼了。ide
=========================================================我是源碼分割線==========================================================post
老規矩,先從對xml的解析講起:ui
/** * 解析typeHandlers節點 */ private void typeHandlerElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { //子節點爲package時,獲取其name屬性的值,而後自動掃描package下的自定義typeHandler if ("package".equals(child.getName())) { String typeHandlerPackage = child.getStringAttribute("name"); typeHandlerRegistry.register(typeHandlerPackage); } else { //子節點爲typeHandler時, 能夠指定javaType屬性, 也能夠指定jdbcType, 也可二者都指定 //javaType 是指定java類型 //jdbcType 是指定jdbc類型(數據庫類型: 如varchar) String javaTypeName = child.getStringAttribute("javaType"); String jdbcTypeName = child.getStringAttribute("jdbcType"); //handler就是咱們配置的typeHandler String handlerTypeName = child.getStringAttribute("handler"); //resolveClass方法就是咱們上篇文章所講的TypeAliasRegistry裏面處理別名的方法 Class<?> javaTypeClass = resolveClass(javaTypeName); //JdbcType是一個枚舉類型,resolveJdbcType方法是在獲取枚舉類型的值 JdbcType jdbcType = resolveJdbcType(jdbcTypeName); Class<?> typeHandlerClass = resolveClass(handlerTypeName); //註冊typeHandler, typeHandler經過TypeHandlerRegistry這個類管理 if (javaTypeClass != null) { if (jdbcType == null) { typeHandlerRegistry.register(javaTypeClass, typeHandlerClass); } else { typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass); } } else { typeHandlerRegistry.register(typeHandlerClass); } } } } }
接下來看看TypeHandler的管理註冊類:this
TypeHandlerRegistry:
/** * typeHandler註冊管理類 */ public final class TypeHandlerRegistry { //源碼一上來,二話不說,幾個大大的HashMap就出現,這不又跟上次講的typeAliases的註冊相似麼 //基本數據類型與其包裝類 private static final Map<Class<?>, Class<?>> reversePrimitiveMap = new HashMap<Class<?>, Class<?>>() { private static final long serialVersionUID = 1L; { put(Byte.class, byte.class); put(Short.class, short.class); put(Integer.class, int.class); put(Long.class, long.class); put(Float.class, float.class); put(Double.class, double.class); put(Boolean.class, boolean.class); put(Character.class, char.class); } }; //這幾個MAP不用說就知道存的是什麼東西吧,命名的好處 private final Map<JdbcType, TypeHandler<?>> JDBC_TYPE_HANDLER_MAP = new EnumMap<JdbcType, TypeHandler<?>>(JdbcType.class); private final Map<Type, Map<JdbcType, TypeHandler<?>>> TYPE_HANDLER_MAP = new HashMap<Type, Map<JdbcType, TypeHandler<?>>>(); private final TypeHandler<Object> UNKNOWN_TYPE_HANDLER = new UnknownTypeHandler(this); private final Map<Class<?>, TypeHandler<?>> ALL_TYPE_HANDLERS_MAP = new HashMap<Class<?>, TypeHandler<?>>(); //就像上篇文章講的typeAliases同樣,mybatis也默認給咱們註冊了很多的typeHandler //具體以下 public TypeHandlerRegistry() { register(Boolean.class, new BooleanTypeHandler()); register(boolean.class, new BooleanTypeHandler()); register(JdbcType.BOOLEAN, new BooleanTypeHandler()); register(JdbcType.BIT, new BooleanTypeHandler()); register(Byte.class, new ByteTypeHandler()); register(byte.class, new ByteTypeHandler()); register(JdbcType.TINYINT, new ByteTypeHandler()); register(Short.class, new ShortTypeHandler()); register(short.class, new ShortTypeHandler()); register(JdbcType.SMALLINT, new ShortTypeHandler()); register(Integer.class, new IntegerTypeHandler()); register(int.class, new IntegerTypeHandler()); register(JdbcType.INTEGER, new IntegerTypeHandler()); register(Long.class, new LongTypeHandler()); register(long.class, new LongTypeHandler()); register(Float.class, new FloatTypeHandler()); register(float.class, new FloatTypeHandler()); register(JdbcType.FLOAT, new FloatTypeHandler()); register(Double.class, new DoubleTypeHandler()); register(double.class, new DoubleTypeHandler()); register(JdbcType.DOUBLE, new DoubleTypeHandler()); register(String.class, new StringTypeHandler()); register(String.class, JdbcType.CHAR, new StringTypeHandler()); register(String.class, JdbcType.CLOB, new ClobTypeHandler()); register(String.class, JdbcType.VARCHAR, new StringTypeHandler()); register(String.class, JdbcType.LONGVARCHAR, new ClobTypeHandler()); register(String.class, JdbcType.NVARCHAR, new NStringTypeHandler()); register(String.class, JdbcType.NCHAR, new NStringTypeHandler()); register(String.class, JdbcType.NCLOB, new NClobTypeHandler()); register(JdbcType.CHAR, new StringTypeHandler()); register(JdbcType.VARCHAR, new StringTypeHandler()); register(JdbcType.CLOB, new ClobTypeHandler()); register(JdbcType.LONGVARCHAR, new ClobTypeHandler()); register(JdbcType.NVARCHAR, new NStringTypeHandler()); register(JdbcType.NCHAR, new NStringTypeHandler()); register(JdbcType.NCLOB, new NClobTypeHandler()); register(Object.class, JdbcType.ARRAY, new ArrayTypeHandler()); register(JdbcType.ARRAY, new ArrayTypeHandler()); register(BigInteger.class, new BigIntegerTypeHandler()); register(JdbcType.BIGINT, new LongTypeHandler()); register(BigDecimal.class, new BigDecimalTypeHandler()); register(JdbcType.REAL, new BigDecimalTypeHandler()); register(JdbcType.DECIMAL, new BigDecimalTypeHandler()); register(JdbcType.NUMERIC, new BigDecimalTypeHandler()); register(Byte[].class, new ByteObjectArrayTypeHandler()); register(Byte[].class, JdbcType.BLOB, new BlobByteObjectArrayTypeHandler()); register(Byte[].class, JdbcType.LONGVARBINARY, new BlobByteObjectArrayTypeHandler()); register(byte[].class, new ByteArrayTypeHandler()); register(byte[].class, JdbcType.BLOB, new BlobTypeHandler()); register(byte[].class, JdbcType.LONGVARBINARY, new BlobTypeHandler()); register(JdbcType.LONGVARBINARY, new BlobTypeHandler()); register(JdbcType.BLOB, new BlobTypeHandler()); register(Object.class, UNKNOWN_TYPE_HANDLER); register(Object.class, JdbcType.OTHER, UNKNOWN_TYPE_HANDLER); register(JdbcType.OTHER, UNKNOWN_TYPE_HANDLER); register(Date.class, new DateTypeHandler()); register(Date.class, JdbcType.DATE, new DateOnlyTypeHandler()); register(Date.class, JdbcType.TIME, new TimeOnlyTypeHandler()); register(JdbcType.TIMESTAMP, new DateTypeHandler()); register(JdbcType.DATE, new DateOnlyTypeHandler()); register(JdbcType.TIME, new TimeOnlyTypeHandler()); register(java.sql.Date.class, new SqlDateTypeHandler()); register(java.sql.Time.class, new SqlTimeTypeHandler()); register(java.sql.Timestamp.class, new SqlTimestampTypeHandler()); // issue #273 register(Character.class, new CharacterTypeHandler()); register(char.class, new CharacterTypeHandler()); } public boolean hasTypeHandler(Class<?> javaType) { return hasTypeHandler(javaType, null); } public boolean hasTypeHandler(TypeReference<?> javaTypeReference) { return hasTypeHandler(javaTypeReference, null); } public boolean hasTypeHandler(Class<?> javaType, JdbcType jdbcType) { return javaType != null && getTypeHandler((Type) javaType, jdbcType) != null; } public boolean hasTypeHandler(TypeReference<?> javaTypeReference, JdbcType jdbcType) { return javaTypeReference != null && getTypeHandler(javaTypeReference, jdbcType) != null; } public TypeHandler<?> getMappingTypeHandler(Class<? extends TypeHandler<?>> handlerType) { return ALL_TYPE_HANDLERS_MAP.get(handlerType); } public <T> TypeHandler<T> getTypeHandler(Class<T> type) { return getTypeHandler((Type) type, null); } public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference) { return getTypeHandler(javaTypeReference, null); } public TypeHandler<?> getTypeHandler(JdbcType jdbcType) { return JDBC_TYPE_HANDLER_MAP.get(jdbcType); } public <T> TypeHandler<T> getTypeHandler(Class<T> type, JdbcType jdbcType) { return getTypeHandler((Type) type, jdbcType); } public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference, JdbcType jdbcType) { return getTypeHandler(javaTypeReference.getRawType(), jdbcType); } private <T> TypeHandler<T> getTypeHandler(Type type, JdbcType jdbcType) { Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = TYPE_HANDLER_MAP.get(type); TypeHandler<?> handler = null; if (jdbcHandlerMap != null) { handler = jdbcHandlerMap.get(jdbcType); if (handler == null) { handler = jdbcHandlerMap.get(null); } } if (handler == null && type != null && type instanceof Class && Enum.class.isAssignableFrom((Class<?>) type)) { handler = new EnumTypeHandler((Class<?>) type); } @SuppressWarnings("unchecked") // type drives generics here TypeHandler<T> returned = (TypeHandler<T>) handler; return returned; } public TypeHandler<Object> getUnknownTypeHandler() { return UNKNOWN_TYPE_HANDLER; } public void register(JdbcType jdbcType, TypeHandler<?> handler) { JDBC_TYPE_HANDLER_MAP.put(jdbcType, handler); } // // REGISTER INSTANCE // /** * 只配置了typeHandler, 沒有配置jdbcType 或者javaType */ @SuppressWarnings("unchecked") public <T> void register(TypeHandler<T> typeHandler) { boolean mappedTypeFound = false; //在自定義typeHandler的時候,能夠加上註解MappedTypes 去指定關聯的javaType //所以,此處須要掃描MappedTypes註解 MappedTypes mappedTypes = typeHandler.getClass().getAnnotation(MappedTypes.class); if (mappedTypes != null) { for (Class<?> handledType : mappedTypes.value()) { register(handledType, typeHandler); mappedTypeFound = true; } } // @since 3.1.0 - try to auto-discover the mapped type if (!mappedTypeFound && typeHandler instanceof TypeReference) { try { TypeReference<T> typeReference = (TypeReference<T>) typeHandler; register(typeReference.getRawType(), typeHandler); mappedTypeFound = true; } catch (Throwable t) { // maybe users define the TypeReference with a different type and are not assignable, so just ignore it } } if (!mappedTypeFound) { register((Class<T>) null, typeHandler); } } /** * 配置了typeHandlerhe和javaType */ public <T> void register(Class<T> javaType, TypeHandler<? extends T> typeHandler) { register((Type) javaType, typeHandler); } private <T> void register(Type javaType, TypeHandler<? extends T> typeHandler) { //掃描註解MappedJdbcTypes MappedJdbcTypes mappedJdbcTypes = typeHandler.getClass().getAnnotation(MappedJdbcTypes.class); if (mappedJdbcTypes != null) { for (JdbcType handledJdbcType : mappedJdbcTypes.value()) { register(javaType, handledJdbcType, typeHandler); } if (mappedJdbcTypes.includeNullJdbcType()) { register(javaType, null, typeHandler); } } else { register(javaType, null, typeHandler); } } public <T> void register(TypeReference<T> javaTypeReference, TypeHandler<? extends T> handler) { register(javaTypeReference.getRawType(), handler); } /** * typeHandlerhe、javaType、jdbcType都配置了 */ public <T> void register(Class<T> type, JdbcType jdbcType, TypeHandler<? extends T> handler) { register((Type) type, jdbcType, handler); } /** * 註冊typeHandler的核心方法 * 就是向Map新增數據而已 */ private void register(Type javaType, JdbcType jdbcType, TypeHandler<?> handler) { if (javaType != null) { Map<JdbcType, TypeHandler<?>> map = TYPE_HANDLER_MAP.get(javaType); if (map == null) { map = new HashMap<JdbcType, TypeHandler<?>>(); TYPE_HANDLER_MAP.put(javaType, map); } map.put(jdbcType, handler); if (reversePrimitiveMap.containsKey(javaType)) { register(reversePrimitiveMap.get(javaType), jdbcType, handler); } } ALL_TYPE_HANDLERS_MAP.put(handler.getClass(), handler); } // // REGISTER CLASS // // Only handler type public void register(Class<?> typeHandlerClass) { boolean mappedTypeFound = false; MappedTypes mappedTypes = typeHandlerClass.getAnnotation(MappedTypes.class); if (mappedTypes != null) { for (Class<?> javaTypeClass : mappedTypes.value()) { register(javaTypeClass, typeHandlerClass); mappedTypeFound = true; } } if (!mappedTypeFound) { register(getInstance(null, typeHandlerClass)); } } // java type + handler type public void register(Class<?> javaTypeClass, Class<?> typeHandlerClass) { register(javaTypeClass, getInstance(javaTypeClass, typeHandlerClass)); } // java type + jdbc type + handler type public void register(Class<?> javaTypeClass, JdbcType jdbcType, Class<?> typeHandlerClass) { register(javaTypeClass, jdbcType, getInstance(javaTypeClass, typeHandlerClass)); } // Construct a handler (used also from Builders) @SuppressWarnings("unchecked") 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); } } /** * 根據指定的pacakge去掃描自定義的typeHander,而後註冊 */ public void register(String packageName) { ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>(); resolverUtil.find(new ResolverUtil.IsA(TypeHandler.class), packageName); Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses(); for (Class<?> type : handlerSet) { //Ignore inner classes and interfaces (including package-info.java) and abstract classes if (!type.isAnonymousClass() && !type.isInterface() && !Modifier.isAbstract(type.getModifiers())) { register(type); } } } // get information /** * 經過configuration對象能夠獲取已註冊的全部typeHandler */ public Collection<TypeHandler<?>> getTypeHandlers() { return Collections.unmodifiableCollection(ALL_TYPE_HANDLERS_MAP.values()); } }
由源碼能夠看到, mybatis爲咱們實現了那麼多TypeHandler, 隨便打開一個TypeHandler,看其源碼,均可以看到,它繼承自一個抽象類:BaseTypeHandler, 那麼咱們是否是也能經過繼承BaseTypeHandler,從而實現自定義的TypeHandler ? 答案是確定的, 那麼如今下面就爲你們演示一下自定義TypeHandler:
=====================================================自定義TypeHandler分割線============================================================
ExampleTypeHandler:
@MappedJdbcTypes(JdbcType.VARCHAR)
//此處若是不用註解指定jdbcType, 那麼,就能夠在配置文件中經過"jdbcType"屬性指定, 同理, javaType 也可經過 @MappedTypes指定 public class ExampleTypeHandler extends BaseTypeHandler<String> { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getString(columnName); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getString(columnIndex); } }
而後,就該配置咱們的自定義TypeHandler了:
<configuration> <typeHandlers> <!-- 因爲自定義的TypeHandler在定義時已經經過註解指定了jdbcType, 因此此處不用再配置jdbcType --> <typeHandler handler="ExampleTypeHandler"/> </typeHandlers> ...... </configuration>
也就是說,咱們在自定義TypeHandler的時候,能夠在TypeHandler經過@MappedJdbcTypes指定jdbcType, 經過 @MappedTypes 指定javaType, 若是沒有使用註解指定,那麼咱們就須要在配置文件中配置。
好啦,本篇文章到此結束。