简介:typeAlias即类型别名,mybatis配置xml映射器时,parameterType和resultType经常使用类的别而不是类的全限定名,TypeAliasRegistry就负责别名到全限定名的映射。
xml映射文件:
全限定名:parameterType="java.lang.Integer"
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
别名:parameterType="integer"
<select id="selectByPrimaryKey" parameterType="integer" resultMap="BaseResultMap">
一、TypeAliasRegistry 源码分析
public class TypeAliasRegistry {
private final Map<String, Class<?>> TYPE_ALIASES = new HashMap(); //记录类型的别名
public TypeAliasRegistry() {
this.registerAlias("string", String.class);
this.registerAlias("byte", Byte.class);
this.registerAlias("long", Long.class);
this.registerAlias("short", Short.class);
this.registerAlias("int", Integer.class);
this.registerAlias("integer", Integer.class);
this.registerAlias("double", Double.class);
//初始化别名映射见下表
......
}
/*
* resolveAlias(String string)解析别名
* */
public <T> Class<T> resolveAlias(String string) {
try {
if (string == null) {
return null;
} else {
String key = string.toLowerCase(Locale.ENGLISH);
Class value;
if (this.TYPE_ALIASES.containsKey(key)) {
value = (Class)this.TYPE_ALIASES.get(key);
} else {
value = Resources.classForName(string);
}
return value;
}
} catch (ClassNotFoundException var4) {
throw new TypeException("Could not resolve type alias '" + string + "'. Cause: " + var4, var4);
}
}
/*
* 注册别名(包扫描),通常对应mybatis中配置:
* <typeAliases>
* <package name="packageName"/>
* </typeAliases>
* */
public void registerAliases(String packageName) {
this.registerAliases(packageName, Object.class);
}
/*
* 注册别名(包扫描),
* */
public void registerAliases(String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil();
//查找“packageName”包下所有的superType的子类
resolverUtil.find(new IsA(superType), packageName);
//获取所有类的Class,
Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
Iterator var5 = typeSet.iterator();
while(var5.hasNext()) {
Class<?> type = (Class)var5.next();
//判断是否已注册
if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
//调用注册方法(类型)
this.registerAlias(type);
}
}
}
/*
* 注册别名(类型),通常对应mybatis中配置:
* <typeAliases>
* <package type="classType"/>
* </typeAliases>
* */
public void registerAlias(Class<?> type) {
//得到类的简写名称,即不含报名的类名称
String alias = type.getSimpleName();
//获取@Alias注解修饰并指定的别名
Alias aliasAnnotation = (Alias)type.getAnnotation(Alias.class);
if (aliasAnnotation != null) {
alias = aliasAnnotation.value();
}
this.registerAlias(alias, type);
}
public void registerAlias(String alias, Class<?> value) {
if (alias == null) {
throw new TypeException("The parameter alias cannot be null");
} else {
//别名统一转为小写应为字母,然后作为map中的键查找
String key = alias.toLowerCase(Locale.ENGLISH);
//判断hashmap中是否已包含别名
if (this.TYPE_ALIASES.containsKey(key) && this.TYPE_ALIASES.get(key) != null && !((Class)this.TYPE_ALIASES.get(key)).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + ((Class)this.TYPE_ALIASES.get(key)).getName() + "'.");
} else {
//注册别名:放入到TYPE_ALIASES中
this.TYPE_ALIASES.put(key, value);
}
}
}
/*
* 注册别名(最核心的方法)
* */
public void registerAlias(String alias, Class<?> value) {
//别名不能为null
if (alias == null) {
throw new TypeException("The parameter alias cannot be null");
} else {
//别名转化为全小写的的英文字母
String key = alias.toLowerCase(Locale.ENGLISH);
//hashmap中查找是否存在该别名
if (this.TYPE_ALIASES.containsKey(key) && this.TYPE_ALIASES.get(key) != null && !((Class)this.TYPE_ALIASES.get(key)).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + ((Class)this.TYPE_ALIASES.get(key)).getName() + "'.");
} else {
//以全小写的别名英文为key,Class为value保存到TYPE_ALIASES中
this.TYPE_ALIASES.put(key, value);
}
}
}
类全限名 | 别名 |
---|---|
String.class | string |
Byte.class | byte |
Long.class | long |
Short.class | short |
Integer.class | int |
Integer.class | integer |
Double.class | double |
Float.class | float |
Boolean.class | boolean |
Byte[].class | byte[] |
Long[].class | long[] |
Short[].class | short[] |
Integer[].class | int[] |
Integer[].class | integer[] |
Double[].class | double[] |
Float[].class | float[] |
Boolean[].class | boolean[] |
Byte.TYPE | _byte |
Long.TYPE | _long |
Short.TYPE | _short |
Integer.TYPE | _int |
Integer.TYPE | _integer |
Double.TYPE | _double |
Float.TYPE | _float |
Boolean.TYPE | _boolean |
byte[].class | _byte[] |
long[].class | _long[] |
short[].class | _short[] |
int[].class | _int[] |
int[].class | _integer[] |
double[].class | _double[] |
float[].class | _float[] |
boolean[].class | _boolean[] |
Date.class | date |
BigDecimal.class | decimal |
BigDecimal.class | bigdecimal |
BigInteger.class | biginteger |
Object.class | object |
Date[].class | date[] |
BigDecimal[].class | decimal[] |
BigDecimal[].class | bigdecimal[] |
BigInteger[].class | biginteger[] |
Object[].class | object[] |
Map.class | map |
HashMap.class | hashmap |
List.class | list |
ArrayList.class | arraylist |
Collection.class | collection |
Iterator.class | iterator |
ResultSet.class | ResultSet |