2.2 typeAlias别名
typeAlias,顾名思义,是类型别名,主要用于简化xml文件的配置。
1、通过别名简化xml配置
默认情况下,我们在xml映射文件中配置的<insert>、<update>、<delete>、<select>元素,parameterType
或resultType
属性都必须指定类的全路径。如:
<insert id="insert" parameterType="com.tianshouzhi.mybatis.quickstart.domain.User"> INSERT INTO user(id,name,age) VALUES (#{id},#{name},#{age}) </insert> <select id="selectById" parameterType="int" resultType="com.tianshouzhi.mybatis.quickstart.domain.User"> select id,name,age from user where id= #{id} </select>
这样的配置长且烦,容易出错。mybatis提供了一个别名系统,让我们在需要填写一个类的全路径时,可以直接用类名替代。
要达到这一点,我们需要mybatis的全局配置文件mybatis-config.xml中,配置<typeAliases>元素,如下所示:
<configuration> <!--列出所有类的别名--> <typeAliases> <!--alias表示别名,type表示类的全路径--> <typeAlias alias="User" type="com.tianshouzhi.mybatis.quickstart.domain.User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="MANAGED" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="shxx12151022" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/UserMapper.xml"/> </mappers> </configuration>
之后,我们可以将<insert>、<select>中parameterType、resultType属性值直接指定为User,配置瞬间清晰了不少。配置如下:
<insert id="insert" parameterType="User"> INSERT INTO user(id,name,age) VALUES (#{id},#{name},#{age}) </insert> <select id="selectById" parameterType="int" resultType="User"> select id,name,age from user where id= #{id} </select>
如果我们有多个类需要起别名,则需要将所有的别名都列出,如:
<typeAliases> <typeAlias alias="User" type="com.tianshouzhi.mybatis.quickstart.domain.User"/> <typeAlias alias="UserAccount" type="com.tianshouzhi.mybatis.quickstart.domain.UserAccount"/> </typeAliases>
特别的,如果这些类都位于同一个包下,我们可以直接通过package
属性指定,不需要逐一列出。
<typeAliases> <package name="com.tianshouzhi.mybatis.quickstart.domain"/> </typeAliases>
默认会使用java实体类首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author 的别名为 author;我们也可以使用@Alias
注解,来指定别名,如:
@Alias("author") public class Author { ... }
2、mybatis 内置别名系统
mybatis除了支持用户自己定义类的别名,实际上mybatis框架本身也内置并大量的使用了别名。例如前面配置<select>元素中,其parameterType属性值为"int";在mybatis-config.xml中配置的<transactionManager type="MANAGED" />、以及 <dataSource type="POOLED">等,type属性用的都是别名。
基本上你可以这样认为,在xml配置中,凡是出现了type或者以type结尾的属性,其值既可以使用类的全路径,也可以使用类的别名。
例如你可以将int改为java.lang.Integer,将POOLED改为org.apache.ibatis.datasource.pooled.PooledDataSourceFactory。这些映射关系都是通过mybatis内置别名系统完成的。
mybatis内置别名系统的别名映射,又可以细分为2类:
mybatis为jdk自带的类的别名映射关系
mybatis框架本身的类的别名映射关系
2.1 mybatis为jdk自带的类的别名映射关系
这个映射列表,在mybatis官方文档上有列出,http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases,如下所示:
需要注意的是,这里列出的映射列表并不全,完整的映射列表位于mybatis的org.apache.ibatis.type.TypeAliasRegistry
类的构造方法中,读者可以自行查看。提醒读者,不必强行记忆这个映射关系,用到的时候知道在哪里查就可以了。
2.2 mybatis框架自带的类的别名映射关系
在前面的mybatis-config.xml中,<transactionManager type="MANAGED" />、以及 <dataSource type="POOLED">等,type属性用的都是别名。这些别名是mybatis为框架本身自带的类其的别名,我们可以在org.apache.ibatis.session.Configuration
这个类的构造方法中,看到mybatis为哪些框架自带的类定义了别名,如下所示。