MyBatis 如何配置多个别名 typeAliasesPackage
作者:程序员牧码 发布时间:2021-11-16 06:35:54
标签:MyBatis,别名,typeAliasesPackage
配置多个别名 typeAliasesPackage
<property name="typeAliasesPackage" value="com.ivan.edu.model,com.ivan.edu.vo"></property>
只需要用逗号“,”隔开就行,当然上面是以 XML 为例,YML 或 Properties 文件配置同理可得~
设置typeAliasesPackage支持**通配符匹配
mybatis的typeAliasesPackage属性的作用是,搜索指定包别名。
配置了以后xml文件中的resultType和parameterType就不需要指定全类名com.example.system.domain.SysUser,我们只需要写SysUser,会到我们配置的typeAliasesPackage包下搜索。
转到MybatisProperties文件中,发现typeAliasesPackage是String类型。
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
/**
* Packages to search type aliases. (Package delimiters are ",; \t\n")
*/
private String typeAliasesPackage;
如果有多个包的话,只能以逗号分隔的形式赋值,如下:
mybatis:
typeAliasesPackage: com.example.system.domain,com.example.common.domain
秉着“不想多敲一点代码”的做法,
我不想每次多一个包,就在typeAliasesPackage后面多加一个包名,
我想要的是可不可以配置一个通配符,就算加再多的包,也不用重新给typeAliasesPackage赋值。
mybatis:
# 规则是,新加的包的名字必须是 com.example.xxx.domain
typeAliasesPackage: com.example.**.domain
如果想要实现上述想法,我们需要自定义SqlSessionFactory,以代码的方式找到匹配com.example.**.domain的所有包名,然后赋值给typeAliasesPackage。
代码实现方式如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.sql.DataSource;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
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;
/**
* Mybatis支持*匹配扫描包
*
* @author ruoyi
*/
@Configuration
public class MyBatisConfig {
@Autowired
private Environment env;
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
/**
* 自定义typeAliasesPackage
* 在application.yml中typeAliasesPackage的值等于com.ruoyi.**.domain
* 但是mybatis是无法识别**通配符的
* 需要我们自己实现通过**通配符匹配到所有的domain包
*
* @param typeAliasesPackage
* @return
*/
public static String setTypeAliasesPackage(String typeAliasesPackage) {
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
List<String> allResult = new ArrayList<String>();
try {
for (String aliasesPackage : typeAliasesPackage.split(",")) {
List<String> result = new ArrayList<String>();
aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resolver.getResources(aliasesPackage);
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> hashResult = new HashSet<String>(result);
allResult.addAll(hashResult);
}
}
if (allResult.size() > 0) {
typeAliasesPackage = String.join(",", (String[]) allResult.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 {
// 获取配置文件中定义的 mybatis.typeAliasesPackage 的值
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
// 获取配置文件中定义的 mybatis.mapperLocations 的值
String mapperLocations = env.getProperty("mybatis.mapperLocations");
// 获取配置文件中定义的 mybatis.configLocation 的值
String configLocation = env.getProperty("mybatis.configLocation");
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
VFS.addImplClass(SpringBootVFS.class);
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
// 在所有jar包的classpath下查找所有以Mapper.xml结尾的xml文件
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
return sessionFactory.getObject();
}
}
来源:https://lux-sun.blog.csdn.net/article/details/106937298
0
投稿
猜你喜欢
- 熬夜写完,尚有不足,但仍在努力学习与总结中,而您的点赞与关注,是对我最大的鼓励!在一些本地化项目开发当中,存在这样一种需求,即开发完成的项目
- 本文实例为大家分享了java实现文件上传下载的具体代码,供大家参考,具体内容如下一.上传1.前端:<form method="
- java 工厂模式的实例详解工厂方法中的“工厂”和我们平常理解的一样:用于生产产品。而客户是要和产品打交道,所以工厂方法模式的意义在于把客户
- import java.io.BufferedReader; import java.io.IOException;  
- 参考视频:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=4通过视频的学习和自身的理解整理出的笔
- [LeetCode] 205. Isomorphic Strings 同构字符串Given two strings s
- 最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的
- 前言Spring Boot 中提供一个全局的配置文件:application.properties,这个配置文件的作用就是,允许我们通过这个
- java Mybatis存进时间戳封装了一个实体类,里面有个字段 Integer createTime。要利用这个实体类将一个时间戳存进数据
- BeanDefinitionRegistryPostProcessor概述可以看到BeanDefinitionRegistryPostPro
- @ConfigurationProperties加载外部配置@ConfigurationProperties可以将外部配置文件(比如appl
- 背景最近好几个项目在运行过程中客户都提出文件上传大小的限制能否设置的大一些,用户经常需要上传好几个G的资料文件,如图纸,视频等,并且需要在上
- 本文实例讲述了Java基于享元模式实现五子棋游戏功能。分享给大家供大家参考,具体如下:一、模式定义享元模式,以共享的方式高效地支持大量的细粒
- 一、spring-boot-devtools在pom中直接引入依赖<dependency> <groupId&
- 在我们日常开发过程中,通常会涉及到数据权限问题,下面以我们常见的一种场景举例:一个公司有很多部门,每个人所处的部门和角色也不同,所以数据权限
- 本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下一、要点设置四个属性android:sin
- springcloud-gateway集成knife4j环境信息环境信息spring-boot:2.6.3spring-cloud-alib
- 1、Java内存模型 Java虚拟机在执行程序时把它管理的内存分为若干数据区域,这些数据区域分布情况如下图所示:程序计数器:一块较小内存区域
- 在分支较多的时候,switch的效率比if高,在反汇编中我们即可看到效率高的原因一、switch语句1、在正向编码时,switch语句可以看
- Flyway是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式。第一步:pom.xml添加maven依赖<!-- https