This commit is contained in:
D 2024-04-20 15:53:34 +08:00
parent 639e1cb9f3
commit ee1f6d9d4f
2 changed files with 133 additions and 138 deletions

View File

@ -1,3 +1,18 @@
# MyBatis配置
# mybatis:
# # 搜索指定包别名
# typeAliasesPackage: com.ruoyi.**.domain
# # 配置mapper的扫描找到所有的mapper.xml映射文件
# mapperLocations: classpath*:mapper/**/*Mapper.xml
# # 加载全局的配置文件
# configLocation: classpath:mybatis/mybatis-config.xml
# PageHelper分页插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql
# MyBatis Plus配置 # MyBatis Plus配置
mybatis-plus: mybatis-plus:
# 搜索指定包别名 # 搜索指定包别名
@ -6,9 +21,3 @@ mybatis-plus:
mapperLocations: classpath*:mapper/**/*Mapper.xml mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件 # 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml configLocation: classpath:mybatis/mybatis-config.xml
# PageHelper分页插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql

View File

@ -1,132 +1,118 @@
//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 org.apache.ibatis.io.VFS; import javax.sql.DataSource;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean; import org.apache.ibatis.io.VFS;
//import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.apache.ibatis.session.SqlSessionFactory;
//import org.springframework.beans.factory.annotation.Autowired; import org.mybatis.spring.SqlSessionFactoryBean;
//import org.springframework.context.annotation.Bean; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
//import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.env.Environment; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.core.io.DefaultResourceLoader; import org.springframework.context.annotation.Bean;
//import org.springframework.core.io.Resource; import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.env.Environment;
//import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.DefaultResourceLoader;
//import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.io.Resource;
//import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.util.ClassUtils; import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
//import com.ruoyi.common.utils.StringUtils; import org.springframework.core.type.classreading.MetadataReader;
// import org.springframework.core.type.classreading.MetadataReaderFactory;
///** import org.springframework.util.ClassUtils;
// * Mybatis支持*匹配扫描包
// * import com.ruoyi.common.utils.StringUtils;
// * @author ruoyi
// */ /**
//@Configuration * Mybatis支持*匹配扫描包
//public class MyBatisConfig *
//{ * @author ruoyi
// @Autowired */
// private Environment env; @Configuration
// @ConditionalOnProperty(prefix = "mybatis", name = { "typeAliasesPackage", "mapperLocations",
// static final String DEFAULT_RESOURCE_PATTERN = "**/*.class"; "configLocation" }, matchIfMissing = false)
// public class MyBatisConfig {
// public static String setTypeAliasesPackage(String typeAliasesPackage) @Autowired
// { private Environment env;
// ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
// MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver); static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
// List<String> allResult = new ArrayList<String>();
// try public static String setTypeAliasesPackage(String typeAliasesPackage) {
// { ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
// for (String aliasesPackage : typeAliasesPackage.split(",")) MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
// { List<String> allResult = new ArrayList<String>();
// List<String> result = new ArrayList<String>(); try {
// aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX for (String aliasesPackage : typeAliasesPackage.split(",")) {
// + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN; List<String> result = new ArrayList<String>();
// Resource[] resources = resolver.getResources(aliasesPackage); aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
// if (resources != null && resources.length > 0) + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/"
// { + DEFAULT_RESOURCE_PATTERN;
// MetadataReader metadataReader = null; Resource[] resources = resolver.getResources(aliasesPackage);
// for (Resource resource : resources) if (resources != null && resources.length > 0) {
// { MetadataReader metadataReader = null;
// if (resource.isReadable()) for (Resource resource : resources) {
// { if (resource.isReadable()) {
// metadataReader = metadataReaderFactory.getMetadataReader(resource); metadataReader = metadataReaderFactory.getMetadataReader(resource);
// try try {
// { result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
// result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName()); .getName());
// } } catch (ClassNotFoundException e) {
// catch (ClassNotFoundException e) e.printStackTrace();
// { }
// e.printStackTrace(); }
// } }
// } }
// } if (result.size() > 0) {
// } HashSet<String> hashResult = new HashSet<String>(result);
// if (result.size() > 0) allResult.addAll(hashResult);
// { }
// HashSet<String> hashResult = new HashSet<String>(result); }
// allResult.addAll(hashResult); if (allResult.size() > 0) {
// } typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
// } } else {
// if (allResult.size() > 0) throw new RuntimeException(
// { "mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
// typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0])); }
// } } catch (IOException e) {
// else e.printStackTrace();
// { }
// throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包"); return typeAliasesPackage;
// } }
// }
// catch (IOException e) public Resource[] resolveMapperLocations(String[] mapperLocations) {
// { ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
// e.printStackTrace(); List<Resource> resources = new ArrayList<Resource>();
// } if (mapperLocations != null) {
// return typeAliasesPackage; for (String mapperLocation : mapperLocations) {
// } try {
// Resource[] mappers = resourceResolver.getResources(mapperLocation);
// public Resource[] resolveMapperLocations(String[] mapperLocations) resources.addAll(Arrays.asList(mappers));
// { } catch (IOException e) {
// ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); // ignore
// List<Resource> resources = new ArrayList<Resource>(); }
// if (mapperLocations != null) }
// { }
// for (String mapperLocation : mapperLocations) return resources.toArray(new Resource[resources.size()]);
// { }
// try
// { @Bean
// Resource[] mappers = resourceResolver.getResources(mapperLocation); public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
// resources.addAll(Arrays.asList(mappers)); String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
// } String mapperLocations = env.getProperty("mybatis.mapperLocations");
// catch (IOException e) String configLocation = env.getProperty("mybatis.configLocation");
// { typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
// // ignore VFS.addImplClass(SpringBootVFS.class);
// }
// } final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// } sessionFactory.setDataSource(dataSource);
// return resources.toArray(new Resource[resources.size()]); sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
// } sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
// sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
// @Bean return sessionFactory.getObject();
// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception }
// { }
// String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
// String mapperLocations = env.getProperty("mybatis.mapperLocations");
// String configLocation = env.getProperty("mybatis.configLocation");
// typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
// VFS.addImplClass(SpringBootVFS.class);
//
// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// sessionFactory.setDataSource(dataSource);
// sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
// sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
// sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
// return sessionFactory.getObject();
// }
//}