标签:rip   blog   article   mybaits   als   htm   pat   首字母   nfa   
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.generator.version>1.3.2</mybatis.generator.version>
MBG
http://www.mybatis.org/spring/apidocs/reference/org/mybatis/spring/SqlSessionFactoryBean.html
 
这几天搭建了spring4.1.2+mybatis3.2.8一个简单的框架。
 
发现mybatis的SqlSessionFactoryBean可以配置typeAliasesPackage属性,自动为domain起别名。
 
如果我的domain在不同包下面,那么这个配置不支持通配符扫描包路径?如下改造:
 
改造前:applicationContext.xml配置:
 
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
-     <property name="dataSource" ref="dataSource" />  
-     <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>  
-     <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>  
-     <property name="typeAliasesPackage" value="com.demo.domain" />  
- </bean>  
 
改造后:applicationContext.xml配置:
 
 
- <bean id="sqlSessionFactory" class="com.demo.core.mybatis.TQSqlSessionFactoryBean">  
-     <property name="dataSource" ref="dataSource" />  
-     <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>  
-     <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>  
-     <property name="typeAliasesPackage" value="com.demo.**.domain" />  
- </bean>  
 
com.demo.core.mybatis.TQSqlSessionFactoryBean类源码:
 
 
- package com.demo.core.mybatis;  
-   
- import java.io.File;  
- import java.io.IOException;  
-   
- import org.mybatis.spring.SqlSessionFactoryBean;  
- import org.slf4j.Logger;  
- import org.slf4j.LoggerFactory;  
- import org.springframework.core.io.Resource;  
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;  
- import org.springframework.core.io.support.ResourcePatternResolver;  
-   
- import com.demo.core.utils.StringUtil;  
-   
- public class TQSqlSessionFactoryBean extends SqlSessionFactoryBean {  
-     Logger logger = LoggerFactory.getLogger(getClass());  
-     private static final String ROOT_PATH = "com" + File.separator + "demo"  
-             + File.separator;  
-     private static final String ROOT_PATH_SPLIT = ",";  
-     private static final String[] PATH_REPLACE_ARRAY = { "]" };  
-   
-     public void setTypeAliasesPackage(String typeAliasesPackage) {  
-         if (!StringUtil.isStringAvaliable(typeAliasesPackage)) {  
-             super.setTypeAliasesPackage(typeAliasesPackage);  
-             return;  
-         }  
-         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();  
-         StringBuffer typeAliasesPackageStringBuffer = new StringBuffer();  
-         try {  
-             for (String location : typeAliasesPackage.split(",")) {  
-                 if (!StringUtil.isStringAvaliable(location)) {  
-                     continue;  
-                 }  
-                 location = "classpath*:"  
-                         + location.trim().replace(".", File.separator);  
-                 typeAliasesPackageStringBuffer.append(getResources(resolver,  
-                         location));  
-             }  
-         } catch (IOException e) {  
-             logger.error(e.getMessage(), e);  
-         }  
-         if ("".equals(typeAliasesPackageStringBuffer.toString())) {  
-             throw new RuntimeException(  
-                     "mybatis typeAliasesPackage 路径扫描错误!请检查applicationContext.xml@sqlSessionFactory配置!");  
-         }  
-         typeAliasesPackage = replaceResult(  
-                 typeAliasesPackageStringBuffer.toString()).replace(  
-                 File.separator, ".");  
-         super.setTypeAliasesPackage(typeAliasesPackage);  
-     }  
-   
-     private String getResources(ResourcePatternResolver resolver,  
-             String location) throws IOException {  
-         StringBuffer resourcePathStringBuffer = new StringBuffer();  
-         for (Resource resource : resolver.getResources(location)) {  
-             String description = resource == null ? "" : resource  
-                     .getDescription();  
-             if (!StringUtil.isStringAvaliable(resource.getDescription())  
-                     || description.indexOf(ROOT_PATH) == -1) {  
-                 continue;  
-             }  
-             resourcePathStringBuffer.append(  
-                     description.substring(description.indexOf(ROOT_PATH)))  
-                     .append(ROOT_PATH_SPLIT);  
-         }  
-         return resourcePathStringBuffer.toString();  
-     }  
-   
-     private String replaceResult(String resultStr) {  
-         for (String replaceStr : PATH_REPLACE_ARRAY) {  
-             resultStr = resultStr.replace(replaceStr, "");  
-         }  
-         return resultStr;  
-     }  
- }  
 
 
 
题外话:
typeAliasesPackage配置路径下的domain中可以添加@org.apache.ibatis.type.Alias(value = "user")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。
http://blog.csdn.net/wsmalltiger/article/details/41825375
 
mybaits3.2.8 别名包扫描通配符
标签:rip   blog   article   mybaits   als   htm   pat   首字母   nfa   
原文地址:http://www.cnblogs.com/softidea/p/6064296.html