mapperLocations配置支持分隔符

This commit is contained in:
RuoYi 2020-11-02 17:34:52 +08:00
parent b779cf053d
commit 082b19e33a
1 changed files with 25 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package com.ruoyi.framework.config;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -21,6 +22,7 @@ import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import com.ruoyi.common.utils.StringUtils;
/** /**
* Mybatis支持*匹配扫描包 * Mybatis支持*匹配扫描包
@ -89,6 +91,28 @@ public class MyBatisConfig
return typeAliasesPackage; return typeAliasesPackage;
} }
public Resource[] resolveMapperLocations(String[] mapperLocations)
{
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<Resource> resources = new ArrayList<Resource>();
if (mapperLocations != null)
{
for (String mapperLocation : mapperLocations)
{
try
{
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
}
catch (IOException e)
{
// ignore
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
@Bean @Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{ {
@ -101,7 +125,7 @@ public class MyBatisConfig
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource); sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage); sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation)); sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
return sessionFactory.getObject(); return sessionFactory.getObject();
} }