Mybatis-配置之别名优化(继Mybatis-配置之属性优化)
1. typeAliases(类型别名)
- 类型别名可为 Java 类型设置一个缩写名字。
- 它仅用于 XML 配置,意在降低冗余的全限定类名书写
<typeAliases>
<typeAlias type="com.kangzhu.pojo.User" alias="User"/>
<package name="com.kangzhu.pojo"/>
</typeAliases>
有两种方式起别名:
- 找到具体的实体类,然后起别名
- 也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,若有注解,则别名为其注解值(扫描实体类的包,它默认的别名就是这个类的类名)
在实体类比较少的时候,使用第一种方式;
如果实体类十分多,建议使用第二种;
第一种可以DIY别名,第二种则不行(但是第二种可以使用注解)
package com.kangzhu.pojo;
import org.apache.ibatis.type.Alias;
@Alias("User")
public class User {
private int id;
private String name;
private String password;
public User() {
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
默认别名:
_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 |