Since the fully qualified name of the class is very long, it is inconvenient to always write such a long name when it needs to be used extensively. In MyBatis, it is allowed to define a shorthand to represent this class, which is an alias, and aliases are divided into system-defined aliases and custom aliases.
Aliases in MyBatis are defined by the class TypeAliasRegistry (org.apache.ibatis.type.TypeAliasRegistry). Note that aliases are not case sensitive in MyBatis.
system defined alias
During the initialization process of MyBatis, the system automatically initializes some aliases, as shown in the following table.
alias |
Java type | Whether to support array |
---|---|---|
_byte |
byte | yes |
_long |
long | yes |
_short |
short | yes |
_int |
int | yes |
_integer |
int | yes |
_double |
double | yes |
_float |
float | yes |
_boolean |
boolean | yes |
string |
String | yes |
byte |
Byte | yes |
long |
Long | yes |
short | Short | yes |
int | Integer | yes |
integer | Integer | yes |
double | Double | yes |
float | Float | yes |
boolean | Boolean | yes |
date | Date | yes |
decimal | BigDecimal | yes |
bigdecimal | BigDecimal | yes |
object | Object | yes |
map | Map | no |
hashmap | HashMap | no |
list | List | no |
arraylist | ArrayList | no |
collection | Collection | no |
iterator | Iterator | no |
ResultSet | ResultSet | no |
If you need to use the array type of the corresponding type, it depends on whether it can support data. If it supports it, you only need to add an alias []
. For example, the alias of the _int array is _int[]. However, aliases like list that do not support arrays cannot be written that way.
Sometimes it is necessary to register aliases through code, let us see how MyBatis initializes these aliases, as shown below.
public TypeAliasRegistry() { registerAlias("string", String.class); registerAlias("byte", Byte.class); registerAlias("long", Long.class); ...... registerAlias("byte[]",Byte[].class); registerAlias("long[]",Long[].class); ...... registerAlias("map", Map.class); registerAlias("hashmap", HashMap.class); registerAlias("list", List.class); registerAlias("arraylist", ArrayList.class); registerAlias("collection", Collection.class); registerAlias("iterator", Iterator.class); registerAlias("ResultSet", ResultSet.class); }
So use the registerAlias method of TypeAliasRegistry to register an alias. Generally, the TypeAliasRegistry class object is obtained through Configuration, and there is a getTypeAliasRegistry method to obtain an alias, such as configuration.getTypeAliasRegistry().
Then you can register the alias through the registerAlias method. In fact, the Configuration object also configures aliases for some commonly used configuration items, as shown below.
//Transaction method alias typeAliasRegistry.registerAlias("JDBC",JdbcTransactionFactory.class); typeAliasRegistry.registerAlias("MANAGED",ManagedTransactionFactory.class); //Data source type alias typeAliasRegistry.registerAlias("JNDI",JndiDataSourceFactory.class); typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class); typeAliasRegistry.registerAlias("UNPOOLED",UnpooledDataSourceFactory.class); //cache policy alias typeAliasRegistry.registerAlias("PERPETUAL",PerpetualCache.class); typeAliasRegistry.reg isterAlias("FIFO ",FifoCache.class); typeAliasRegistry.registerAlias("LRU",LruCache.class); typeAliasRegistry.registerAlias("SOFT", SoftCache.class); typeAliasRegistry.registerAlias("WEAK", WeakCache.class); //Database identification name typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class); //language driver class name typeAliasRegistry.registerAlias("XML",XMLLanguageDriver.class); typeAliasRegistry.registerAlias("RAW",RawLanguageDriver.class); //日志类别名 typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class); typeAliasRegistry.registerAlias("COMMONS_LOGGTNG",JakartmCommonsLogginglmpl.class); typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class); typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class); typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class); typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class); typeAliasRegistry.registerAlias("NO_LOGGING",NoLoggingImpl.class); //动态代理别名 typeAliasRegistry.registerAlias("CGLIB",CglibProxyFactory.class); typeAliasRegistry.registerAlias("JAVASSIST",JavassistProxyFactory.class);
These configurations are to make it easier for us to configure MyBatis related information. The above are the aliases defined by the MyBatis system. When we use them, don't repeat the names, which may cause other problems.
custom alias
Since there are many objects in reality, especially in large-scale Internet systems, such as the user (User) object, which sometimes needs to be used repeatedly, MyBatis also provides rules for user-defined aliases. We can register through the registerAlias method of the TypeAliasRegistry class, or use configuration files or scan methods to customize it.
Using a configuration file definition is simple:
<typeAliases><!--别名-->
<typeAlias alias="role" type="com.mybatis.po.Role"/>
<typeAlias alias="role" type="com.mybatis.po.User"/>
</typeAliases>
In this way, an alias can be defined. If there are many classes that need to define aliases, it is not so easy to configure in this way. MyBatis also supports scanning aliases. For example, the above two classes are under the package com.mybatis.po, then they can be defined as:
<typeAliases><!--别名-->
<package name="com.mybatis.po"/>
</typeAliases>
In this way, MyBatis will scan the classes in this package, and change its first letter to a lowercase as its alias. For example, the alias of the class Role will be changed to role, and the alias of User will be changed to user. Using such a rule, sometimes there will be duplicate names.
For example, for the class com.mybatis.po.User, MyBatis also adds scanning for the package com.mybatis.po, and an exception will occur. At this time, you can use the annotation @Alias ("user3") provided by MyBatis to distinguish, as follows shown.
package com.mybatis.po; @Alias("user3") public Class User { ...... }
In this way, the problem of scanning failure caused by duplicate aliases can be avoided.