Mybatis 自定义SqlSessionFactoryBean扫描通配符typeAliasesPackage
typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决
- 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();
- }
- }
- }
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的配置文件中修改
- <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>

<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的更多相关文章
- mybaits3.2.8 别名包扫描通配符
<mybatis.version>3.2.8</mybatis.version><mybatis.spring.version>1.2.2</mybatis. ...
- Mybatis3.2不支持Ant通配符TypeAliasesPackage扫描的解决方案
业务场景 业务场景:首先项目进行分布式拆分之后,按照模块再分为为api层和service层,web层. 其中订单业务的实体类放在com.muses.taoshop.item.entity,而用户相关的 ...
- Mybatis自定义SQL拦截器
本博客介绍的是继承Mybatis提供的Interface接口,自定义拦截器,然后将项目中的sql拦截一下,打印到控制台. 先自定义一个拦截器 package com.muses.taoshop.com ...
- spring与mybatis整合(扫描Mapper接口)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...
- Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean
问题描述: 一月 15, 2014 3:43:13 下午 org.springframework.context.support.AbstractApplicationContext prepareR ...
- Mybatis自定义分布式二级缓存实现与遇到的一些问题解决方案!
先说两句: 我们都知道Mybatis缓存分两类: 一级缓存(同一个Session会话内) & 二级缓存(基于HashMap实现的以 namespace为范围的缓存) 今天呢, 我们不谈一级缓存 ...
- springboot整合mybatis遇到无法扫描MaperScan包的问题
1.启动类加上@MaperScan注解后,一直报错如下: Error creating bean with name 'platUserMapper' defined in file [D:\work ...
- MyBatis Spring SqlSessionFactoryBean 配置
在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中,则使用 SqlSessionFactory ...
- 利用Sonar定制自定义JS扫描规则(二)——自定义JS扫描规则
在上一篇blog中,我们将sonar几个需要的环境都搭建好了,包括sonar的服务器,sonar runner,sonar的javascript插件.现在我们就来讲如何自定义JS扫描规则. 实际上有3 ...
随机推荐
- 委托、Lambda表达式、事件系列06,使用Action实现观察者模式,体验委托和事件的区别
在"实现观察者模式(Observer Pattern)的2种方式"中,曾经通过接口的方式.委托与事件的方式实现过观察者模式.本篇体验使用Action实现此模式,并从中体验委托与事件 ...
- 【Devops】【docker】【CI/CD】Jenkins源码管理,设置gitlab上项目的clone地址 + jenkins构建报错:Please make sure you have the correct access rights and the repository exists.
注意,如果 jenkins构建报错:Please make sure you have the correct access rights and the repository exists. 而此时 ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- java数字转换成文字方法
public class IntToSmallChineseNumber { public static String ToCH(int intInput) { String si = String. ...
- H2:开源内存数据库引擎
本资源由 伯乐在线 - 刘立华 整理 H2是一个开源的内存数据库.Java编写.快速.小巧(1.5MB jar包)还提供了Web控制台管理数据库内容. 主要功能 非常快速的数据库引擎. 开源. Jav ...
- windows 安装ninja
ninja连接: https://ninja-build.org/ https://github.com/ninja-build/ninja 下载安装包: https://github.com/nin ...
- 【转】各种 NoSQL 的比较
转自 : http://linux.cn/article-2177-1.html 来源:Linux中国 原文:http://linux.cn/article-2177-1.html 即使关系 ...
- [转]Nginx 502 PHP LNMP 502 终极解决方案 完美解决502 用 upstream 和 fastcgi_next_upstream 可以极大缓解
转: http://xn--ghqyhzj.com/post-21537.html 本文针对LNMP的PHP 版本ver 5.3.6 or Higher,其它未测试过. 1. 使用不同端口或php-f ...
- 启明星请假系统appform:流程在线帮助
启明星默认员工角色都是空的,对于空的角色,系统使用staff角色. 也就是,默认员工都是staff. 因此,你只要定义主管和经理角色即可. 例如: staff-manage-hr operator
- Spring Boot工程结构推荐程结构(最佳实践)
工程结构(最佳实践) Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程 ...