原文始发于:09. Mybatis 核心配置-别名配置
在映射文件中, 对于parameterType 和 resultType 默认需要写类的全限定名称, 设置别名之后, 可以使用类的简单类名.
1. 别名配置
- 全局别名配置, 在Mybatis 核心配置文件中进行配置. 配置标签为typeAlias.
- typeAlias 中子标签typeAlias 和package 的配置顺序是有要求的, 必须typeAlias 在前
- Mybatis 别名不区分大小写.
1.1 typeAlias-为单个类配置别名
- typeAlias 可以为具体的某个类配置别名, 默认别名为类名首字母小写.
- typeAlias 属性可自定义别名, 不使用类名首字母小写作为别名
<typeAliases> <!-- 为具体类设置别名 --> <typeAlias type="org.zongf.learn.mybatis3.l01.po.EmployeePO" alias="employeePO"/> <typeAlias type="org.zongf.learn.mybatis3.l01.po.DepartmentPO" /> </typeAliases>
1.2 package-批量配置别名
- pacakge 标签可对某个包及其子包下所有类创建别名, 别名名称为类名首字母小写
- 需要注意的是当不同包下类名一致时, 会引发别名冲突
<typeAliases> <!-- 为某个包下所有类设置别名 --> <package name="org.zongf.learn.mybatis3.l01.po" /> <package name="org.zongf.learn.mybatis3.l01.vo" /> </typeAliases>
1.3 @Alias-处理别名冲突
- 当别名冲突时, 可以为其中的一个类, 添加@TypeAlias 注解, 自定义别名, 解决冲突
- @Alias 注解: org.apache.ibatis.type.Alias
@Alias("emp") public class EmployeePO { }
2.内置别名
Mybatis 内置了一些别名, 自定义别名时, 需要注意别冲突了.
内置别名 | java 类型 |
---|---|
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
官方文档地址: http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases