TypeAlias可以帮助我们在Mybatis配置文件中直接引用类别名,而不必使用类完全限定名那么冗长的名字。TypeAliasRegistry、Configuration实例化时会注册默认的类别名,方便我们使用。类别名忽略大小写,在注册类别名或根据类别名或去类时,Mybatis会将别名转换成小写。
TypeHandlerRegistry帮助我们将Java类型和数据库类型进行转换。一个Java类型可以对应多个数据库类型。
注册类别名或根据别名获取类都会将别名转成小写,TypeAliasRegistry实例化时会注册常用类别名。
public void registerAliases(String packageName){
registerAliases(packageName, Object.class);
}
public void registerAliases(String packageName, Class> superType){
// 获取指定包下执行类型的所有类
ResolverUtil
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
Set
for(Class> type : typeSet){
// 忽略内部类、接口、抽象类(包括package-info.java)
if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
registerAlias(type);
}
}
}
public void registerAlias(Class> type) {
// 获取类名
String alias = type.getSimpleName();
// 获取类上的@Alias注解
Alias aliasAnnotation = type.getAnnotation(Alias.class);
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
registerAlias(alias, type);
}
// value若为NULL,后续相同别名的会被覆盖。如果没有获取时返回NULL,需要注意处理。
public void registerAlias(String alias, Class> value) {
if (alias == null) {
throw new TypeException("The parameter alias cannot be null");
}
// 别名转成小写
String key = alias.toLowerCase(Locale.ENGLISH);
if (TYPE_ALIASES.containsKey(key) && TYPE_ALIASES.get(key) != null && !TYPE_ALIASES.get(key).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(key).getName() + "'.");
}
TYPE_ALIASES.put(key, value);
}
public
try {
if (string == null) {
return null;
}
// 别名转成小写
String key = string.toLowerCase(Locale.ENGLISH);
Class
if (TYPE_ALIASES.containsKey(key)) {
value = (Class
} else {
value = (Class
}
return value;
} catch (ClassNotFoundException e) {
throw new TypeException("Could not resolve type alias '" + string + "'. Cause: " + e, e);
}
}
public
if (javaTypeClass != null) {
try {
// 需要Java类型参数
Constructor> c = typeHandlerClass.getConstructor(Class.class);
return (TypeHandler
} catch (NoSuchMethodException ignored) {
// ignored
} catch (Exception e) {
throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, e);
}
}
try {
Constructor> c = typeHandlerClass.getConstructor();
return (TypeHandler
} catch (Exception e) {
throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e);
}
}
// 获取typeHandler Class上的@MappedTypes(Java类型)
public void register(Class> typeHandlerClass) {
boolean mappedTypeFound = false;
// 获取typeHandler Class上的@MappedTypes(Java类型)
MappedTypes mappedTypes = typeHandlerClass.getAnnotation(MappedTypes.class);
if (mappedTypes != null) {
for (Class> javaTypeClass : mappedTypes.value()) {
// 传送门:5.3
register(javaTypeClass, typeHandlerClass);
mappedTypeFound = true;
}
}
// 没有@MappedType即没有指定Java类型
if (!mappedTypeFound) {
// 传送门:5.4
register(getInstance(null, typeHandlerClass));
}
}
public void register(Class> javaTypeClass, Class> typeHandlerClass) {
// 传送门:5.5
register(javaTypeClass, getInstance(javaTypeClass, typeHandlerClass));
}
public
boolean mappedTypeFound = false;
// 获取typeHandler 实例上的@MappedTypes(Java类型)
MappedTypes mappedTypes = typeHandler.getClass().getAnnotation(MappedTypes.class);
if (mappedTypes != null) {
for (Class> handledType : mappedTypes.value()) {
// 传送门:5.5
register(handledType, typeHandler);
mappedTypeFound = true;
}
}
// @since 3.1.0 - try to auto-discover the mapped type
if (!mappedTypeFound && typeHandler instanceof TypeReference) {
try {
TypeReference
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) {
// 传送门:5.5
register((Class
}
private
// 获取typeHandler 实例上的@MappedJdbcTypes(JDBC类型)
MappedJdbcTypes mappedJdbcTypes = typeHandler.getClass().getAnnotation(MappedJdbcTypes.class);
if (mappedJdbcTypes != null) {
for (JdbcType handledJdbcType : mappedJdbcTypes.value()) {
// 传送门:5.6
register(javaType, handledJdbcType, typeHandler);
}
if (mappedJdbcTypes.includeNullJdbcType()) {
// 传送门:5.6
register(javaType, null, typeHandler);
}
} else {
// 传送门:5.6
register(javaType, null, typeHandler);
}
}
// 注册TypeHandler Class --> TypeHandler 实例映射关系,如果JavaType不为空,注册JavaType --> Map
private void register(Type javaType, JdbcType jdbcType, TypeHandler> handler) {
if (javaType != null) {
Map
if (map == null || map == NULL_TYPE_HANDLER_MAP) {
map = new HashMap
// 一个Java类型可能对应多个TypeHandler
TYPE_HANDLER_MAP.put(javaType, map);
}
map.put(jdbcType, handler);
}
// TypeHandler Class --> TypeHandler 实例
ALL_TYPE_HANDLERS_MAP.put(handler.getClass(), handler);
}
public void register(String packageName) {
// 扫描指定包下实现TypeHandler接口的类
ResolverUtil
resolverUtil.find(new ResolverUtil.IsA(TypeHandler.class), packageName);
Set
for (Class> type : handlerSet) {
// 忽略内部类和接口(包括package-info.java)和抽象类
if (!type.isAnonymousClass() && !type.isInterface() && !Modifier.isAbstract(type.getModifiers())) {
// 传送门:5.2
register(type);
}
}
}