MyBatis typeAliases元素标签(含注解方式)及其属性、设置

版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/88375261

个人建议还是采用全类名的方式,这样可以很轻松的看到该类的所有方法等,比较方便直观;这样不过也有缺点,不利于维护等。

简介

typeAliases:别名处理器,可以为java类型(resultType)起别名。类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。

注意:在不指定别名情况下,别名默认就是类名小写。某些情况下别名不区分大小写。

通过单个定义别名的方式

语法

<typeAliases>
<!--
    alias:定义的别名
    type:指定要起别名的类型全类名
    默认别名就是类名小写
-->
  <typeAlias alias="别名" type="指定要起别名的类型全类名"/>
 
</typeAliases>

例如

<typeAliases>
<!--
    alias:定义的别名
    type:指定要起别名的类型全类名
    默认别名就是类名小写
-->
  <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>

当这样配置时,Blog可以用在任何使用domain.blog.Blog的地方。

通过包扫描的方式

该方式将扫描该包下的所有类,默认为类名的小写。

注意:若该包下面还用子包,并且子包与该包有相同的类名,则需要为其中一个类进行注解,否则将会报错(冲突),因为mybatis不知道是哪个包,必须明确的指明才可以。

语法

<typeAliases>
  <package name="包名"/>
</typeAliases>

例如

<typeAliases>
  <package name="domain.blog"/>
</typeAliases>

采用注解的方式

每一个在包 domain.blog 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author 的别名为 author;若有注解,则别名为其注解值。

例子:

@Alias("author")
public class Author {
    ...
}

常见的 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

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/88375261