zoukankan      html  css  js  c++  java
  • mybaits3.2.8 别名包扫描通配符

    <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配置:

    [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")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。

    http://blog.csdn.net/wsmalltiger/article/details/41825375

  • 相关阅读:
    Web开发人员必备工具-Emmet (Zen Coding)
    Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
    jquery 插件站
    数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统
    类似wordpress的网站模板
    浅谈 SOAP
    在Livemedia的基础上开发自己的流媒体客户端 V 0.01
    模式识别之分类器knn---c语言实现带训练数据---反余弦匹配
    多项式相乘快速算法原理及相应C代码实现---用到fft
    模式识别之基础---常用分类算法特性归纳
  • 原文地址:https://www.cnblogs.com/softidea/p/6064296.html
Copyright © 2011-2022 走看看