Mybatis——TypeAliasRegistry 類型別名註冊

簡介: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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章