mybaits3.2.8 别名包扫描通配符

这几天搭建了spring4.1.2+mybatis3.2.8一个简单的框架。


发现mybatis的SqlSessionFactoryBean可以配置typeAliasesPackage属性,自动为domain起别名。


如果我的domain在不同包下面,那么这个配置不支持通配符扫描包路径?如下改造:


改造前:applicationContext.xml配置:

[html]  view plain  copy
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.     <property name="dataSource" ref="dataSource" />  
  3.     <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>  
  4.     <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>  
  5.     <property name="typeAliasesPackage" value="com.demo.domain" />  
  6. </bean>  

改造后:applicationContext.xml配置:

[html]  view plain  copy
  1. <bean id="sqlSessionFactory" class="com.demo.core.mybatis.TQSqlSessionFactoryBean">  
  2.     <property name="dataSource" ref="dataSource" />  
  3.     <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>  
  4.     <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>  
  5.     <property name="typeAliasesPackage" value="com.demo.**.domain" />  
  6. </bean>  

com. demo .core.mybatis.TQSqlSessionFactoryBean类源码:

[java]  view plain  copy
  1. package com.demo.core.mybatis;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.mybatis.spring.SqlSessionFactoryBean;  
  7. import org.slf4j.Logger;  
  8. import org.slf4j.LoggerFactory;  
  9. import org.springframework.core.io.Resource;  
  10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;  
  11. import org.springframework.core.io.support.ResourcePatternResolver;  
  12.   
  13. import com.demo.core.utils.StringUtil;  
  14.   
  15. /** 
  16.  * @ClassName: TQSqlSessionFactoryBean 
  17.  * @Description: mybatis自动扫描别名路径(新增通配符匹配功能) 
  18.  * @author wangxiaohu wsmalltiger@163.com 
  19.  * @date 2014年12月9日 上午9:36:23 
  20.  */  
  21. public class TQSqlSessionFactoryBean extends SqlSessionFactoryBean {  
  22.     Logger logger = LoggerFactory.getLogger(getClass());  
  23.     private static final String ROOT_PATH = "com" + File.separator + "demo"  
  24.             + File.separator;  
  25.     private static final String ROOT_PATH_SPLIT = ",";  
  26.     private static final String[] PATH_REPLACE_ARRAY = { "]" };  
  27.   
  28.     public void setTypeAliasesPackage(String typeAliasesPackage) {  
  29.         if (!StringUtil.isStringAvaliable(typeAliasesPackage)) {  
  30.             super.setTypeAliasesPackage(typeAliasesPackage);  
  31.             return;  
  32.         }  
  33.         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();  
  34.         StringBuffer typeAliasesPackageStringBuffer = new StringBuffer();  
  35.         try {  
  36.             for (String location : typeAliasesPackage.split(",")) {  
  37.                 if (!StringUtil.isStringAvaliable(location)) {  
  38.                     continue;  
  39.                 }  
  40.                 location = "classpath*:"  
  41.                         + location.trim().replace(".", File.separator);  
  42.                 typeAliasesPackageStringBuffer.append(getResources(resolver,  
  43.                         location));  
  44.             }  
  45.         } catch (IOException e) {  
  46.             logger.error(e.getMessage(), e);  
  47.         }  
  48.         if ("".equals(typeAliasesPackageStringBuffer.toString())) {  
  49.             throw new RuntimeException(  
  50.                     "mybatis typeAliasesPackage 路径扫描错误!请检查applicationContext.xml@sqlSessionFactory配置!");  
  51.         }  
  52.         typeAliasesPackage = replaceResult(  
  53.                 typeAliasesPackageStringBuffer.toString()).replace(  
  54.                 File.separator, ".");  
  55.         super.setTypeAliasesPackage(typeAliasesPackage);  
  56.     }  
  57.   
  58.     private String getResources(ResourcePatternResolver resolver,  
  59.             String location) throws IOException {  
  60.         StringBuffer resourcePathStringBuffer = new StringBuffer();  
  61.         for (Resource resource : resolver.getResources(location)) {  
  62.             String description = resource == null ? "" : resource  
  63.                     .getDescription();  
  64.             if (!StringUtil.isStringAvaliable(resource.getDescription())  
  65.                     || description.indexOf(ROOT_PATH) == -1) {  
  66.                 continue;  
  67.             }  
  68.             resourcePathStringBuffer.append(  
  69.                     description.substring(description.indexOf(ROOT_PATH)))  
  70.                     .append(ROOT_PATH_SPLIT);  
  71.         }  
  72.         return resourcePathStringBuffer.toString();  
  73.     }  
  74.   
  75.     private String replaceResult(String resultStr) {  
  76.         for (String replaceStr : PATH_REPLACE_ARRAY) {  
  77.             resultStr = resultStr.replace(replaceStr, "");  
  78.         }  
  79.         return resultStr;  
  80.     }  
  81. }  


题外话:

typeAliasesPackage配置路径下的domain中可以添加@org.apache.ibatis.type.Alias(value = "user")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。



此处记录!欢迎大家提出宝贵意见!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值