版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描
本篇内容主要讲解“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让在下来带大家学习“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”吧!mybatis默认配置typeAliasesPackage是不支持正则扫描package的,因此需要手动继承org.mybatis.spring.SqlSessionFactoryBean,自己实现正则扫描,方法和传统的spring+mybatis没什么区别,不同的是一个需要继承类一个是使用的扫描实现。对于两个或多个扫描路径,例:perties配置Mybatis如下mybatis.typeAliasesPackage=.onethird.*.*.entity
mybatis.mapperLocations=classpath:mapper/*.xmlpackage
.onethird.nursinghome;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.HashSet;
import
java.util.List;
import
java.util.Properties;
import
javax.sql.DataSource;
import
org.apache.ibatis.session.SqlSessionFactory;
import
org.mybatis.spring.SqlSessionFactoryBean;
import
org.mybatis.spring.annotation.MapperScan;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.SpringApplication;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.context.annotation.PropertySource;
import
org.springframework.core.env.Environment;
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
com.Application;
/**
*
*
@Title:
MyBatisConfig.java
*
*
@Package
*
@Description:
mybtis实现正则扫描java
bean包
*
*
@author
*
@date
*
@version
V1.0
*/
@Configuration
@PropertySource("classpath:perties")
public
class
MyBatisConfig
{
@Autowired
private
Environment
env;
static
final
String
DEFAULT_RESOURCE_PATTERN
=
"**/*.class";
public
static
String
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;
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)
{
HashSet<String>
h
=
new
HashSet<String>(result);
result.clear();
result.addAll(h);
typeAliasesPackage
=
String.join(","
,(String[])
result.toArray(new
String[0]));
}
else
{
throw
new
RuntimeException(
"mybatis
typeAliasesPackage
路径扫描错误,
参数typeAliasesPackage:"
+
typeAliasesPackage
+
"未找到任何包");
}
}
catch
(IOException
e)
{
e.printStackTrace();
}
return
typeAliasesPackage;
}
@Bean
public
SqlSessionFactory
sqlSessionFactory(DataSource
dataSource)
throws
Exception
{
System.out.println(">>>>>>>>>>>配置[typeAliasesPackage,mapperLocations]START>>>>>>>>>>>>>>");
Properties
props
=
new
Properties();
String
typeAliasesPackage
=
env
.getProperty("mybatis.typeAliasesPackage");
String
mapperLocations
=
env.getProperty("mybatis.mapperLocations");
typeAliasesPackage=setTypeAliasesPackage(typeAliasesPackage);
final
SqlSessionFactoryBean
sessionFactory
=
new
SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources(mapperLocations));
System.out.println(">>>>>>>>>>>配置[typeAliasesPackage,mapperLocations]END>>>>>>>>>>>>>>");
return
sessionFactory.getObject();
}
public
static
void
main(String[]
args)
throws
Exception
{
setTypeAliasesPackage(".onethird.*.*.entity");
}
}Mybatis自定义扫描通配符typeAliasesPackagetypeAliasesPackage默认只能扫描某一个路径下,或以逗号等分割的几个路径下的内容,不支持通配符和正则,采用重写的方式解决package
mon;
import
com.xxxx.xxx.util.LogUtil;
import
mons.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+",未找到任何包");
}
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 技巧动作组合(教案) 体育六年级下册
- 矿井优化设计课程设计
- 平移 教学设计-2023-2024学年三年级下册数学北师大版
- 山东省临淄外国语实验学校届九年级信息技术上册 第一单元 第3课 个人计算器教学设计
- 心理健康教案心理能力培养
- 给小汽车加油课程设计
- 辽宁省沈阳市和平区2024年中考数学一模试题含解析
- 爱妈妈课程设计
- 安工大热轧课程设计
- 节日彩灯课程设计程序
- 《水电水利工程施工监理规范》
- 语文园地二 逐字稿 五年级上册语文
- 第三单元找质数(学案)五年级上册数学北师大版
- 第3单元第3课时《雁群飞》(课件)人音版 音乐一年级下册
- 急救护理学-第四章-重症监护
- 奢侈品的了解课件
- 2024年10月自考00024普通逻辑押题(必考题型)汇总含答案解析
- 足下垂预防与护理课件
- 颅内压增高及脑疝护理课件
- 《电脑动画》复习备考题库及答案(汇总)
- 奶粉购销合同范本简单
评论
0/150
提交评论