Commit 97190e77 by zhaoqingwei

阿拉丁-魔登

parent 737b8fd8
...@@ -17,6 +17,7 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy; ...@@ -17,6 +17,7 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy(exposeProxy = true) @EnableAspectJAutoProxy(exposeProxy = true)
// 指定要扫描的Mapper类的包的路径 // 指定要扫描的Mapper类的包的路径
@MapperScan("com.ruoyi.project.**.mapper") @MapperScan("com.ruoyi.project.**.mapper")
@MapperScan(value = {"com.ruoyi.project.**.mapper", "com.ruoyi.adsdesk.mapper"})
public class ApplicationConfig public class ApplicationConfig
{ {
/** /**
......
package com.ruoyi.framework.config; //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.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;
import org.apache.ibatis.io.VFS; //import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory; //import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean; //import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; //import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; //import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader; //import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; //import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; //import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; //import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory; //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; //import com.ruoyi.common.utils.StringUtils;
//
/** ///**
* Mybatis支持*匹配扫描包 // * Mybatis支持*匹配扫描包
* // *
* @author ruoyi // * @author ruoyi
*/ // */
@Configuration //@Configuration
public class MyBatisConfig //public class MyBatisConfig
{ //{
@Autowired // @Autowired
private Environment env; // private Environment env;
//
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; // static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
//
public static String setTypeAliasesPackage(String typeAliasesPackage) // public static String setTypeAliasesPackage(String typeAliasesPackage)
{ // {
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); // ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); // MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
List<String> allResult = new ArrayList<String>(); // List<String> allResult = new ArrayList<String>();
try // try
{ // {
for (String aliasesPackage : typeAliasesPackage.split(",")) // for (String aliasesPackage : typeAliasesPackage.split(","))
{ // {
List<String> result = new ArrayList<String>(); // List<String> result = new ArrayList<String>();
aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX // aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; // + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resolver.getResources(aliasesPackage); // Resource[] resources = resolver.getResources(aliasesPackage);
if (resources != null && resources.length > 0) // if (resources != null && resources.length > 0)
{ // {
MetadataReader metadataReader = null; // MetadataReader metadataReader = null;
for (Resource resource : resources) // for (Resource resource : resources)
{ // {
if (resource.isReadable()) // if (resource.isReadable())
{ // {
metadataReader = metadataReaderFactory.getMetadataReader(resource); // metadataReader = metadataReaderFactory.getMetadataReader(resource);
try // try
{ // {
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); // result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
} // }
catch (ClassNotFoundException e) // catch (ClassNotFoundException e)
{ // {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
} // }
} // }
if (result.size() > 0) // if (result.size() > 0)
{ // {
HashSet<String> hashResult = new HashSet<String>(result); // HashSet<String> hashResult = new HashSet<String>(result);
allResult.addAll(hashResult); // allResult.addAll(hashResult);
} // }
} // }
if (allResult.size() > 0) // if (allResult.size() > 0)
{ // {
typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); // typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
} // }
else // else
{ // {
throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); // throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
} // }
} // }
catch (IOException e) // catch (IOException e)
{ // {
e.printStackTrace(); // e.printStackTrace();
} // }
return typeAliasesPackage; // return typeAliasesPackage;
} // }
//
public Resource[] resolveMapperLocations(String[] mapperLocations) // public Resource[] resolveMapperLocations(String[] mapperLocations)
{ // {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); // ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<Resource> resources = new ArrayList<Resource>(); // List<Resource> resources = new ArrayList<Resource>();
if (mapperLocations != null) // if (mapperLocations != null)
{ // {
for (String mapperLocation : mapperLocations) // for (String mapperLocation : mapperLocations)
{ // {
try // try
{ // {
Resource[] mappers = resourceResolver.getResources(mapperLocation); // Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers)); // resources.addAll(Arrays.asList(mappers));
} // }
catch (IOException e) // catch (IOException e)
{ // {
// ignore // // ignore
} // }
} // }
} // }
return resources.toArray(new Resource[resources.size()]); // return resources.toArray(new Resource[resources.size()]);
} // }
//
@Bean // @Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception // public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
{ // {
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); // String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
String mapperLocations = env.getProperty("mybatis.mapperLocations"); // String mapperLocations = env.getProperty("mybatis.mapperLocations");
String configLocation = env.getProperty("mybatis.configLocation"); // String configLocation = env.getProperty("mybatis.configLocation");
typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); // typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
VFS.addImplClass(SpringBootVFS.class); // VFS.addImplClass(SpringBootVFS.class);
//
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource); // sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage); // sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(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();
} // }
} //}
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment