typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决

  1. package com.xxxx.xxx.util.common;
  2. import com.xxxx.xxx.util.LogUtil;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.mybatis.spring.SqlSessionFactoryBean;
  5. import org.slf4j.Logger;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  8. import org.springframework.core.io.support.ResourcePatternResolver;
  9. import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
  10. import org.springframework.core.type.classreading.MetadataReader;
  11. import org.springframework.core.type.classreading.MetadataReaderFactory;
  12. import org.springframework.util.ClassUtils;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. /**
  17. * Created by Administrator on 2015/10/6.
  18. */
  19. public class PackagesSqlSessionFactoryBean extends SqlSessionFactoryBean {
  20. static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
  21. private static Logger logger = LogUtil.get();
  22. @Override
  23. public void setTypeAliasesPackage(String typeAliasesPackage) {
  24. ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
  25. MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
  26. typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
  27. ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/" + DEFAULT_RESOURCE_PATTERN;
  28. //将加载多个绝对匹配的所有Resource
  29. //将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分
  30. //然后进行遍历模式匹配
  31. try {
  32. List<String> result = new ArrayList<String>();
  33. Resource[] resources =  resolver.getResources(typeAliasesPackage);
  34. if(resources != null && resources.length > 0){
  35. MetadataReader metadataReader = null;
  36. for(Resource resource : resources){
  37. if(resource.isReadable()){
  38. metadataReader =  metadataReaderFactory.getMetadataReader(resource);
  39. try {
  40. result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
  41. } catch (ClassNotFoundException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }
  47. if(result.size() > 0) {
  48. super.setTypeAliasesPackage(StringUtils.join(result.toArray(), ","));
  49. }else{
  50. logger.warn("参数typeAliasesPackage:"+typeAliasesPackage+",未找到任何包");
  51. }
  52. //logger.info("d");
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
package com.xxxx.xxx.util.common;  

import com.xxxx.xxx.util.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; /**
* Created by Administrator on 2015/10/6.
*/
public class PackagesSqlSessionFactoryBean extends SqlSessionFactoryBean { static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; private static Logger logger = LogUtil.get(); @Override
public void setTypeAliasesPackage(String typeAliasesPackage) {
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(typeAliasesPackage) + "/" + DEFAULT_RESOURCE_PATTERN; //将加载多个绝对匹配的所有Resource
//将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分
//然后进行遍历模式匹配
try {
List<String> result = new ArrayList<String>();
Resource[] resources = resolver.getResources(typeAliasesPackage);
if(resources != null && resources.length > 0){
MetadataReader metadataReader = null;
for(Resource resource : resources){
if(resource.isReadable()){
metadataReader = metadataReaderFactory.getMetadataReader(resource);
try {
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
if(result.size() > 0) {
super.setTypeAliasesPackage(StringUtils.join(result.toArray(), ","));
}else{
logger.warn("参数typeAliasesPackage:"+typeAliasesPackage+",未找到任何包");
}
//logger.info("d");
} catch (IOException e) {
e.printStackTrace();
}
} }

然后在mybatis的配置文件中修改

  1. <bean id="sqlSession" class="com.xxxx.xxxx.util.common.PackagesSqlSessionFactoryBean">
  2. <property name="configLocation" value="classpath:config/sqlmap/sqlmap-config.xml" />
  3. <property name="dataSource" ref="dataSource"/>
  4. <!--<property name="mapperLocations"-->
  5. <!--value="classpath*:com/xxxx/xxxx/merchant/**/domain/mapper/*.xml"/>-->
  6. <property name="typeAliasesPackage" value="com.xxxx.xxxx.custom.*.domain"/>
  7. <property name="plugins">
  8. <array>
  9. <ref bean="pageInterceptor"/>
  10. </array>
  11. </property>
  12. </bean>
  13. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  14. <property name="basePackage" value="com.xxxx.xxxx.**.dao"/>
  15. </bean>
<bean id="sqlSession" class="com.xxxx.xxxx.util.common.PackagesSqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/sqlmap/sqlmap-config.xml" />
<property name="dataSource" ref="dataSource"/>
<!--<property name="mapperLocations"-->
<!--value="classpath*:com/xxxx/xxxx/merchant/**/domain/mapper/*.xml"/>-->
<property name="typeAliasesPackage" value="com.xxxx.xxxx.custom.*.domain"/>
<property name="plugins">
<array>
<ref bean="pageInterceptor"/>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxxx.xxxx.**.dao"/>
</bean>


Mybatis 自定义SqlSessionFactoryBean扫描通配符typeAliasesPackage的更多相关文章

  1. mybaits3.2.8 别名包扫描通配符

    <mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis. ...

  2. Mybatis3.2不支持Ant通配符TypeAliasesPackage扫描的解决方案

    业务场景 业务场景:首先项目进行分布式拆分之后,按照模块再分为为api层和service层,web层. 其中订单业务的实体类放在com.muses.taoshop.item.entity,而用户相关的 ...

  3. Mybatis自定义SQL拦截器

    本博客介绍的是继承Mybatis提供的Interface接口,自定义拦截器,然后将项目中的sql拦截一下,打印到控制台. 先自定义一个拦截器 package com.muses.taoshop.com ...

  4. spring与mybatis整合(扫描Mapper接口)

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...

  5. Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

    问题描述: 一月 15, 2014 3:43:13 下午 org.springframework.context.support.AbstractApplicationContext prepareR ...

  6. Mybatis自定义分布式二级缓存实现与遇到的一些问题解决方案!

    先说两句: 我们都知道Mybatis缓存分两类: 一级缓存(同一个Session会话内) & 二级缓存(基于HashMap实现的以 namespace为范围的缓存) 今天呢, 我们不谈一级缓存 ...

  7. springboot整合mybatis遇到无法扫描MaperScan包的问题

    1.启动类加上@MaperScan注解后,一直报错如下: Error creating bean with name 'platUserMapper' defined in file [D:\work ...

  8. MyBatis Spring SqlSessionFactoryBean 配置

    在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中,则使用 SqlSessionFactory ...

  9. 利用Sonar定制自定义JS扫描规则(二)——自定义JS扫描规则

    在上一篇blog中,我们将sonar几个需要的环境都搭建好了,包括sonar的服务器,sonar runner,sonar的javascript插件.现在我们就来讲如何自定义JS扫描规则. 实际上有3 ...

随机推荐

  1. delete_failed_internal_error安装失败

    一.错误提示 二.最简单的解决方式 使用Android Studio (IDEA 2018.3.5)进行开发时,无论是模拟器还是,USB连接手机,都是无法安装和启动APP.后来发现是IDEA中,设置的 ...

  2. java根据模板HTML动态生成PDF

    原文:https://segmentfault.com/a/1190000009160184 一.需求说明:根据业务需要,需要在服务器端生成可动态配置的PDF文档,方便数据可视化查看. 二.解决方案: ...

  3. 【IntelliJ IDEA】idea或者JetBrains公司所有编辑器,设置其软件的字体样式

    操作如下: 修改完成后的效果: 可以看到修改以后的ide的效果:

  4. Linux、apache 无法使用PHP创建目录和文件

    因为项目的需要,这几天搭建了虚拟机,环境是centos7+lamp,可是搭建好网站后,即使把权限放开了(777),我试了改父文件夹权限:重新创建文件夹,改权限再移动文件:更换文件夹的属主.统统不行.这 ...

  5. 来自Google的TCP BBR拥塞控制算法解析

    转自:http://blog.csdn.net/dog250/article/details/52830576 写本文的初衷一部分来自于工作,更多的来自于发现国内几乎还没有中文版的关于TCP bbr算 ...

  6. linux find 10天内改动过的文件

    find . -name "*.h" -mtime -10 -type f -print find . -regex ".*\.\(c\|h\)" -mtime ...

  7. 用开源项目PhotoView实现图片的双指缩放和双击放大缩小

    项目地址:https://github.com/chrisbanes/PhotoView 用开源项目有个好处,一是实现简单,二是bug少.那么我们就来说下这个项目能够实现的效果: 1.单个图片的双指缩 ...

  8. Mock 模拟测试简介及 Mockito 使用入门

    Mock 是什么mock 测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法.这个虚拟的对象就是mock对象.mock对象就是真实对象在调试期间的代 ...

  9. warning: LF will be replaced by CRLF in dubbo-demo-api/pom.xml.

    今天使用git add .的时候出现了一个错误. 错误如下: 解决方案: $ rm -rf .git // 删除.git $ git config --global core.autocrlf fal ...

  10. JavaScript:String 对象

    ylbtech-JavaScript:String 对象 1.返回顶部 String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); ...