别名设置
在写SQL语句或者在写结果集时,会遇到表名、列名、类的全名太长的情况,Mybatis专门提供了一个标签来设置别名<typeAliases>
<!-- more -->
示例:
<typeAliases>
<!-- 批量为实体类添加别名 指定包名 Mybatis之后会自动为该包下的类设置别名 -->
<package name="com.example.mybatis"/>
</typeAliases>
<!-- 设置别名之前使用实体类的示例 -->
<resultMap id="baseResult" type="com.example.mybatis.User">
<association property="role" javaType="com.example.mybatis.Role">
</association>
</resultMap>
<!-- 设置别名之后使用实体类的示例 -->
<resultMap id="baseResult1" type="user">
<association property="role" javaType="role">
</association>
</resultMap>
当然别名也可以单个设置
<typeAliases>
<typeAlias alias="user" type="com.example.mybatis.User"/>
<typeAlias alias="role" type="com.example.mybatis.Role"/>
</typeAliases>
也可以使用注解进行配置别名
@Alias("user")
public class User{
}
TypeAliasRegistry
上面讲述了别名的用处以及配置,Mybatis怎么管理的别名呢?
使用的TypeAliasRegistry来进行注册和管理的
首先对于一些基本的类型,Mybatis已经默认设置了别名了
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);
this.registerAlias("float", Float.class);
this.registerAlias("boolean", Boolean.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);
this.registerAlias("float[]", Float[].class);
this.registerAlias("boolean[]", Boolean[].class);
this.registerAlias("_byte", Byte.TYPE);
this.registerAlias("_long", Long.TYPE);
this.registerAlias("_short", Short.TYPE);
this.registerAlias("_int", Integer.TYPE);
this.registerAlias("_integer", Integer.TYPE);
this.registerAlias("_double", Double.TYPE);
this.registerAlias("_float", Float.TYPE);
this.registerAlias("_boolean", Boolean.TYPE);
this.registerAlias("_byte[]", byte[].class);
this.registerAlias("_long[]", long[].class);
this.registerAlias("_short[]", short[].class);
this.registerAlias("_int[]", int[].class);
this.registerAlias("_integer[]", int[].class);
this.registerAlias("_double[]", double[].class);
this.registerAlias("_float[]", float[].class);
this.registerAlias("_boolean[]", boolean[].class);
this.registerAlias("date", Date.class);
this.registerAlias("decimal", BigDecimal.class);
this.registerAlias("bigdecimal", BigDecimal.class);
this.registerAlias("biginteger", BigInteger.class);
this.registerAlias("object", Object.class);
this.registerAlias("date[]", Date[].class);
this.registerAlias("decimal[]", BigDecimal[].class);
this.registerAlias("bigdecimal[]", BigDecimal[].class);
this.registerAlias("biginteger[]", BigInteger[].class);
this.registerAlias("object[]", Object[].class);
this.registerAlias("map", Map.class);
this.registerAlias("hashmap", HashMap.class);
this.registerAlias("list", List.class);
this.registerAlias("arraylist", ArrayList.class);
this.registerAlias("collection", Collection.class);
this.registerAlias("iterator", Iterator.class);
this.registerAlias("ResultSet", ResultSet.class);
}
TypeAliasRegistry中包含了对别名的注册和解析
别名注册
对于别名注册的方法为registerAliases,有几个重载方法
注意:在注册的时候,其实存到map中的key全都是小写的
//整个包进行别名设置 会扫描包下的所有类
public void registerAliases(String packageName) {
this.registerAliases(packageName, Object.class);
}
//整个包进行别名设置 会扫描包下的所有类
public void registerAliases(String packageName, Class<?> superType) {
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil();
// 查找指定包下的superType类型的类
resolverUtil.find(new IsA(superType), packageName);
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);
}
}
}
// 使用@Alias注解设置别名的方法
public void registerAlias(Class<?> type) {
// 根据代码可以看到,在不给出确定的别名时,mybatis给的默认别名都是类名
String alias = type.getSimpleName();
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 {
// 别名转换为小写
String key = alias.toLowerCase(Locale.ENGLISH);
if (this.typeAliases.containsKey(key) && this.typeAliases.get(key) != null && !((Class)this.typeAliases.get(key)).equals(value)) {
throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + ((Class)this.typeAliases.get(key)).getName() + "'.");
} else {
this.typeAliases.put(key, value);
}
}
}
public void registerAlias(String alias, String value) {
try {
this.registerAlias(alias, Resources.classForName(value));
} catch (ClassNotFoundException var4) {
throw new TypeException("Error registering type alias " + alias + " for " + value + ". Cause: " + var4, var4);
}
}
别名解析
别名解析的方法为resolveAlias
public <T> Class<T> resolveAlias(String string) {
try {
if (string == null) {
return null;
} else {
// 这个对照着别名注册时 也是将key值设定为小写的
String key = string.toLowerCase(Locale.ENGLISH);
Class value;
if (this.typeAliases.containsKey(key)) {
value = (Class)this.typeAliases.get(key);
} else {
value = Resources.classForName(string);
}
return value;
}
} catch (ClassNotFoundException var4) {
throw new TypeException("Could not resolve type alias '" + string + "'. Cause: " + var4, var4);
}
}
https://zhhll.icu/2020/框架/mybatis/组件分析/3.mybatis之别名设置/
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。