MyBatis中typealiases的使用

转自:http://blog.csdn.net/lelewenzibin/article/details/42713585

问题描述

Mybatis有个代码生成工具,生成的代码里面有mapper.xml文件,mapper.xml中的sql语句会用parameterType这个属性,而这个值可能是我们自定义的对象,此时,如果没有typealiases,我们就需要为parameterType指定全路径:

[html] view plain copy
 
  1. <span style="font-size:14px;"><select id="getStudent" parameterType="com.csct.domain.StudentEntity" resultType="com.csct.domain.StudentEntity" resultMap="studentResultMap"></span>  

代码自动生成工具生成的mapper.xml文件中不带全路径,那么,如果有三、四十个实体对象,我们岂不是要修改的地方太多了。

解决办法

这个时候,我们是不是很期望有一个方式,能把com.csct.domain下的实体全部给加载进来,mapper.xml遇到不识别的类就在这里查找呢?你都想到了,Mybatis当然也能想到吧,typealiases就是为此而生的。使用特简单,如下:

[html] view plain copy
 
  1. <span style="font-size:14px;"><configuration>  
  2.     <typeAliases>  
  3.       <!--  
  4.       通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,  
  5.       并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名。  
  6.       也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user)   
  7.       <package name="com.dy.entity"/>  
  8.        -->  
  9.       <typeAlias alias="UserEntity" type="com.dy.entity.User"/>  
  10.   </typeAliases>  
  11.     
  12.   ......  
  13.     
  14. </configuration></span>  


这样,mapper.xml的sql语句就如下了:

[java] view plain copy
 
  1. <span style="font-size:14px;"><select id="getStudent" parameterType="StudentEntity" resultType="StudentEntity" resultMap="studentResultMap"></span>  



原文地址:https://www.cnblogs.com/YuyuanNo1/p/7873220.html