MyBatis 如何配置多个别名 typeAliasesPackage

网友投稿 2423 2022-11-07

MyBatis 如何配置多个别名 typeAliasesPackage

MyBatis 如何配置多个别名 typeAliasesPackage

目录配置多个别名 typeAliasesPackage设置typeAliasesPackage支持**通配符匹配

配置多个别名 typeAliasesPackage

只需要用逗号“,”隔开就行,当然上面是以 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的值等于comhttp://.ruoyi.**.domain

* 但是mybatis是无法识别**通配符的

* 需要我们自己实现通过**通配符匹配到所有的domain包

*

* @param typeAliasesPackage

* @return

*/

public static String setTypeAliasesPackage(String typeAliasesPackage) {

ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();

MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);

List allResult = new ArrayList();

try {

for (String aliasesPackage : typeAliasesPackage.split(",")) {

List result = new ArrayList();

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.getMhttp://etadataReader(resource);

try {

result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

}

if (result.size() > 0) {

HashSet hashResult = new HashSet(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();

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CYQ.Data 轻量数据层之路 使用篇三曲 MAction 取值赋值(十四)
下一篇:Linq入门
相关文章

 发表评论

暂时没有评论,来抢沙发吧~