MyBatis 如何配置多個(gè)別名 typeAliasesPackage
配置多個(gè)別名 typeAliasesPackage
<property name="typeAliasesPackage" value="com.ivan.edu.model,com.ivan.edu.vo"></property>
只需要用逗號(hào)“,”隔開就行,當(dāng)然上面是以 XML 為例,YML 或 Properties 文件配置同理可得~
設(shè)置typeAliasesPackage支持**通配符匹配
mybatis的typeAliasesPackage屬性的作用是,搜索指定包別名。
配置了以后xml文件中的resultType和parameterType就不需要指定全類名com.example.system.domain.SysUser,我們只需要寫SysUser,會(huì)到我們配置的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;
如果有多個(gè)包的話,只能以逗號(hào)分隔的形式賦值,如下:
mybatis: ?? ?typeAliasesPackage: com.example.system.domain,com.example.common.domain
秉著“不想多敲一點(diǎn)代碼”的做法,
我不想每次多一個(gè)包,就在typeAliasesPackage后面多加一個(gè)包名,
我想要的是可不可以配置一個(gè)通配符,就算加再多的包,也不用重新給typeAliasesPackage賦值。
mybatis: ?? ?# 規(guī)則是,新加的包的名字必須是 com.example.xxx.domain ?? ?typeAliasesPackage: com.example.**.domain
如果想要實(shí)現(xiàn)上述想法,我們需要自定義SqlSessionFactory,以代碼的方式找到匹配com.example.**.domain的所有包名,然后賦值給typeAliasesPackage。
代碼實(shí)現(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是無(wú)法識(shí)別**通配符的 * 需要我們自己實(shí)現(xiàn)通過(guò)**通配符匹配到所有的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 路徑掃描錯(cuò)誤,參數(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(); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java獲取接口的所有實(shí)現(xiàn)類方法總結(jié)示例
這篇文章主要給大家介紹了關(guān)于Java獲取接口的所有實(shí)現(xiàn)類方法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06關(guān)于MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解
這篇文章主要介紹了MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解,具有很好的參考價(jià)值,希望大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問(wèn)題及解決方案
這篇文章主要介紹了springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問(wèn)題及解決方案,親測(cè)試過(guò)可以,需要的朋友可以參考下2018-07-07Java中類與對(duì)象的相關(guān)知識(shí)點(diǎn)總結(jié)
對(duì)象是類實(shí)例化出來(lái)的,對(duì)象中含有類的屬性,類是對(duì)象的抽象,下面這篇文章主要給大家介紹了關(guān)于Java中類與對(duì)象的一些相關(guān)知識(shí)點(diǎn),需要的朋友可以參考下2021-11-11springboot2.2.2集成dubbo的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot2.2.2集成dubbo的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01java 反射和動(dòng)態(tài)代理詳解及實(shí)例代碼
這篇文章主要介紹了java 反射和動(dòng)態(tài)代理詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09idea集成shell運(yùn)行環(huán)境以及shell輸出中文亂碼的解決
這篇文章主要介紹了idea集成shell運(yùn)行環(huán)境以及shell輸出中文亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08MyBatisPlus利用Service實(shí)現(xiàn)獲取數(shù)據(jù)列表
這篇文章主要為大家詳細(xì)介紹了怎樣使用 IServer 提供的 list 方法查詢多條數(shù)據(jù),這些方法將根據(jù)查詢條件獲取多條數(shù)據(jù),感興趣的可以了解一下2022-06-06基于hibernate框架在eclipse下的配置方法(必看篇)
下面小編就為大家?guī)?lái)一篇基于hibernate框架在eclipse下的配置方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09