带你学习Mybatis之别名设置



别名设置

在写SQL语句或者在写结果集时,会遇到表名、列名、类的全名太长的情况,Mybatis专门提供了一个标签来设置别名<typeAliases>

示例:

<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);
        }
    }

参考文献

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值