MyBatis 如何配置多個別名 typeAliasesPackage
配置多個別名 typeAliasesPackage
<property name="typeAliasesPackage" value="com.ivan.edu.model,com.ivan.edu.vo"></property>
只需要用逗號“,”隔開就行,當(dāng)然上面是以 XML 為例,YML 或 Properties 文件配置同理可得~
設(shè)置typeAliasesPackage支持**通配符匹配
mybatis的typeAliasesPackage屬性的作用是,搜索指定包別名。
配置了以后xml文件中的resultType和parameterType就不需要指定全類名com.example.system.domain.SysUser,我們只需要寫SysUser,會到我們配置的typeAliasesPackage包下搜索。
轉(zhuǎn)到MybatisProperties文件中,發(fā)現(xiàn)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: ?? ?# 規(guī)則是,新加的包的名字必須是 com.example.xxx.domain ?? ?typeAliasesPackage: com.example.**.domain
如果想要實現(xiàn)上述想法,我們需要自定義SqlSessionFactory,以代碼的方式找到匹配com.example.**.domain的所有包名,然后賦值給typeAliasesPackage。
代碼實現(xiàn)方式如下:
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是無法識別**通配符的
* 需要我們自己實現(xiàn)通過**通配符匹配到所有的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 路徑掃描錯誤,參數(shù)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結(jié)尾的xml文件
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
return sessionFactory.getObject();
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Visual?Studio?Code配置Tomcat運行Java?Web項目詳細步驟
VS Code是一款非常棒的文本編輯器,具有配置簡單、功能豐富、輕量簡潔的特點,并且極其適合處理中小規(guī)模的代碼,這篇文章主要給大家介紹了關(guān)于Visual?Studio?Code配置Tomcat運行Java?Web項目的詳細步驟,需要的朋友可以參考下2023-11-11
Java獲取網(wǎng)頁數(shù)據(jù)步驟方法詳解
這篇文章主要介紹了Java獲取網(wǎng)頁數(shù)據(jù)步驟方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Java設(shè)計模式之觀察者模式(Observer模式)
這篇文章主要介紹了Java設(shè)計模式之觀察者模式(Observer模式),文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
SpringMVC攔截器創(chuàng)建配置及執(zhí)行順序
這篇文章主要為大家介紹了SpringMVC攔截器創(chuàng)建配置及執(zhí)行順序,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Java實現(xiàn)網(wǎng)絡(luò)文件下載以及下載到指定目錄
在Spring框架中,StreamUtils和FileCopyUtils兩個工具類提供了方便的文件下載功能,它們都屬于org.springframework.util包,可以通過簡單的方法調(diào)用實現(xiàn)文件流的復(fù)制和下載,這些工具類支持多種參數(shù)傳遞,涵蓋了文件下載的多種場景2024-09-09
使用webservice自定義注解處理參數(shù)加解密問題
這篇文章主要介紹了使用webservice自定義注解處理參數(shù)加解密問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

