在Java中,类型别名(Type Alias)是一个用于简化某个类型的名称的方式。它在XML配置中特别有用,可以减少在全限定类名上繁琐的重复输入。例如:
<typeAliases><typeAlias alias="Author" type="domain.blog.Author"/><typeAlias alias="Blog" type="domain.blog.Blog"/><typeAlias alias="Comment" type="domain.blog.Comment"/><typeAlias alias="Post" type="domain.blog.Post"/><typeAlias alias="Section" type="domain.blog.Section"/><typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases> 通过这个配置,可以在任何需要使用 domain.blog.Blog的地方,直接使用 Blog来代替。
在MyBatis中,你可以指定一个包,在这个包中,MyBatis将会搜索需要使用的Java Bean类。例如:
<typeAliases><package name="domain.blog"/>
</typeAliases> 在 domain.blog包中的每个Java Bean,如果没有找到注释,将会使用非限定类名的首字母小写形式注册为别名。例如,domain.blog.Author会被注册为 author。如果找到了 @Alias注释,它的值将被用作别名。请看下面的示例:
@Alias("author")
public class Author {...
}在MyBatis中有许多内置的类型别名,用于常见的Java类型。它们都是不区分大小写的,特别注意原始类型的特殊处理,由于其重载的名称。
| Alias | Mapped Type | 
|---|---|
| _byte | byte | 
| _char (since 3.5.10) | char | 
| _character (since 3.5.10) | char | 
| _long | long | 
| _short | short | 
| _int | int | 
| _integer | int | 
| _double | double | 
| _float | float | 
| _boolean | boolean | 
| string | String | 
| byte | Byte | 
| char (since 3.5.10) | Character | 
| character (since 3.5.10) | Character | 
| long | Long | 
| short | Short | 
| int | Integer | 
| integer | Integer | 
| double | Double | 
| float | Float | 
| boolean | Boolean | 
| date | Date | 
| decimal | BigDecimal | 
| bigdecimal | BigDecimal | 
| biginteger | BigInteger | 
| object | Object | 
| date[] | Date[] | 
| decimal[] | BigDecimal[] | 
| bigdecimal[] | BigDecimal[] | 
| biginteger[] | BigInteger[] | 
| object[] | Object[] | 
| map | Map | 
| hashmap | HashMap | 
| list | List | 
| arraylist | ArrayList | 
| collection | Collection | 
| iterator | Iterator |