MyBatis配置------typeAliases 别名

        由于类的全限定名称很长,需要大量使用的时候,总写那么长的名称不方便。在 MyBatis 中允许定义一个简写来代表这个类,这就是别名,别名分为系统定义别名和自定义别名。在 MyBatis 中别名由 TypeAliasRegis(org.apache.ibatis.type.TypeAliasRegistry)去定义。 注意,在 MyBatis 中别名不区分大小写。

1. 系统定义别名

        在MyBatis 的初始化过程中,系统自动初始化了别名。可参考官网类型别名

        如果需要使用对应类型的数组型,要看其是否能支持数据,如果支持只需要使用别名加[]即可,比如一int 数组的别名就是_int[]。而类似 list 这样不支持数组的别名,则不能那么写。 有时候要通过代码来实现注册别名。TypeAliasRegistry初始化别名:

public TypeAliasRegistry() {
	this.registerAlias("string", String.class);
	this.registerAlias("byte", Byte.class);
	this.registerAlias("long", Long.class);
    ......
	this.registerAlias("byte[]", Byte[].class);
	this.registerAlias("long[]", Long[].class);
    ......
	this.registerAlias("map", Map.class);
	this.registerAlias("hashmap", HashMap.class);
	this.registerAlias("list", List.class);
	this.registerAlias("arraylist", ArrayList.class);
	this.registerAlias("collection", Collection.class);
	this.registerAlias("iterator", Iterator.class);
	this.registerAlias("ResultSet", ResultSet.class);
}

        所以使用 TypeAliasRegistry 的 registerAlias 方法就可以注册别名了 。一般是通过 Configuration 获取 TypeAliasRegistry 类对象,其中有一个getTypeAliasRegistry 方法可以获得别名,如 configuration.getTypeAliasRegistry()。然后就可以通过 registerAlias 方法对别名注册了。而事实上 Configuration 对象也对一些常用的配置项配置了别名。Configuration配置的别名:

// 事务方式别名
this.typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
this.typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
// 数据源类型别名
this.typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
this.typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
this.typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);
// 缓存策略别名
this.typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
this.typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
this.typeAliasRegistry.registerAlias("LRU", LruCache.class);
this.typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
this.typeAliasRegistry.registerAlias("WEAK", WeakCache.class);
// 数据库标识别名
this.typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);
// 语言驱动类别名
this.typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
this.typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);
// 日志类别名
this.typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
this.typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
this.typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
this.typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
this.typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
this.typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
this.typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);
// 动态代理别名
this.typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
this.typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);

        这些配置为的是更容易配置 MyBatis 相关信息。

        以上就是 MyBatis 系统定义的别名,在使用的时候,不要重复命名,导致出现其他问题。

2. 自定义别名

        由于现实中,特别是大型互联网系统中存在许多对象,比如用户(User)这个对象有时候需要大量重复地使用,因此 MyBatis 也提供了用户自定义别名的规则。可以通过 TypeAliasRegistry 类的 registerAlias 方法注册,也可以采用配置文件或者扫描方式来自定义它。

        使用配置文件定义很简单:

<typeAliases><!--别名-->
	<typeAlias alias="role type com.learn.ssm.chapter4.pojo.Role"/>
	<typeAlias alias="role type com.learn.ssm chapter4.p0jo.User"/>
</typeAliases>

        这样就可以定义一个别名了。如果有很多类需要定义别名,那么用这样的方式进行配置可就不那么轻松了。 MyBatis 还支持扫描别名。比如上面的两个类都在包 com.leam.ssm.chapter4.pojo 之下 ,那么就可以定义为:

<typeAliases><!--别名-->
	<package name="com.learn.ssm.chapter4.pojo"/>
</typeAliases>

        这样 MyBatis 将扫描这个包里面的类,将其第一个字母变为小写作为其别名,比如类 Role 的别名会变为 role ,而 User 的别名会变为 user 。使用这样的规则,有时候会出现重名, 比如 com.learn.ssm.chapter3.pojo.User 这个类, MyBatis 还增加了对包 com.leam. ssm. chapter3.pojo 的扫描,那么就会出现异常,这个时候可以使用 MyBatis 提供的 @Alias("user3")进行区分:

package com.learn.ssm.chapter3.pojo; 
@Alias("user3") 
public Class User {
    ......
}

        这样就能够避免因为别名重名导致的扫描失败的问题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值