update 修复mybaits-plus集成的bug
This commit is contained in:
parent
70842d9f8e
commit
ea6adbf8a3
7
pom.xml
7
pom.xml
@ -25,7 +25,7 @@
|
|||||||
<bitwalker.version>1.21</bitwalker.version>
|
<bitwalker.version>1.21</bitwalker.version>
|
||||||
<swagger.version>3.0.0</swagger.version>
|
<swagger.version>3.0.0</swagger.version>
|
||||||
<kaptcha.version>2.3.3</kaptcha.version>
|
<kaptcha.version>2.3.3</kaptcha.version>
|
||||||
<pagehelper.boot.version>1.4.7</pagehelper.boot.version>
|
<pagehelper.boot.version>2.1.0</pagehelper.boot.version>
|
||||||
<fastjson.version>2.0.45</fastjson.version>
|
<fastjson.version>2.0.45</fastjson.version>
|
||||||
<jackson.version>2.9.8</jackson.version>
|
<jackson.version>2.9.8</jackson.version>
|
||||||
<oshi.version>6.4.11</oshi.version>
|
<oshi.version>6.4.11</oshi.version>
|
||||||
@ -63,6 +63,11 @@
|
|||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
<version>${mybatis-spring-boot.version}</version>
|
<version>${mybatis-spring-boot.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
<version>3.5.16</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- jaxb 处理xml -->
|
<!-- jaxb 处理xml -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
# PageHelper分页插件
|
||||||
|
pagehelper:
|
||||||
|
helperDialect: mysql
|
||||||
|
supportMethodsArguments: true
|
||||||
|
params: count=countSql
|
||||||
|
|
||||||
|
createSqlSessionFactory:
|
||||||
|
use: mybatis
|
||||||
|
|
||||||
# MyBatis配置
|
# MyBatis配置
|
||||||
mybatis:
|
mybatis:
|
||||||
# 搜索指定包别名
|
# 搜索指定包别名
|
||||||
@ -7,12 +16,6 @@ mybatis:
|
|||||||
# 加载全局的配置文件
|
# 加载全局的配置文件
|
||||||
configLocation: classpath:mybatis/mybatis-config.xml
|
configLocation: classpath:mybatis/mybatis-config.xml
|
||||||
|
|
||||||
# PageHelper分页插件
|
|
||||||
pagehelper:
|
|
||||||
helperDialect: mysql
|
|
||||||
supportMethodsArguments: true
|
|
||||||
params: count=countSql
|
|
||||||
|
|
||||||
# MyBatis Plus配置
|
# MyBatis Plus配置
|
||||||
mybatis-plus:
|
mybatis-plus:
|
||||||
# 搜索指定包别名
|
# 搜索指定包别名
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.ruoyi.common.interceptor.mybatis;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
public interface CreateSqlSessionFactory {
|
||||||
|
public SqlSessionFactory createSqlSessionFactory(Environment env, DataSource dataSource) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class MybatisUtils {
|
||||||
|
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
|
||||||
|
|
||||||
|
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 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return typeAliasesPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static 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()]);
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,6 @@
|
|||||||
package com.ruoyi.framework.config;
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
@ -17,21 +12,15 @@ import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
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.context.annotation.Primary;
|
|
||||||
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.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;
|
|
||||||
|
|
||||||
import com.ruoyi.common.enums.DataSourceType;
|
import com.ruoyi.common.enums.DataSourceType;
|
||||||
|
import com.ruoyi.common.interceptor.mybatis.CreateSqlSessionFactory;
|
||||||
|
import com.ruoyi.common.utils.MybatisUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
import com.ruoyi.framework.datasource.DynamicSqlSessionTemplate;
|
import com.ruoyi.framework.datasource.DynamicSqlSessionTemplate;
|
||||||
@ -44,95 +33,28 @@ import com.ruoyi.framework.datasource.DynamicSqlSessionTemplate;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class MyBatisConfig {
|
public class MyBatisConfig {
|
||||||
|
|
||||||
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
|
|
||||||
|
|
||||||
Logger logger = LoggerFactory.getLogger(MyBatisConfig.class);
|
Logger logger = LoggerFactory.getLogger(MyBatisConfig.class);
|
||||||
|
|
||||||
public static String setTypeAliasesPackage(String typeAliasesPackage) {
|
@Bean
|
||||||
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
|
@ConditionalOnProperty(prefix = "createSqlSessionFactory", name = "use", havingValue = "mybatis")
|
||||||
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
|
public CreateSqlSessionFactory createSqlSessionFactory() {
|
||||||
List<String> allResult = new ArrayList<String>();
|
return new CreateSqlSessionFactory() {
|
||||||
try {
|
public SqlSessionFactory createSqlSessionFactory(Environment env, DataSource dataSource) throws Exception {
|
||||||
for (String aliasesPackage : typeAliasesPackage.split(",")) {
|
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
|
||||||
List<String> result = new ArrayList<String>();
|
String mapperLocations = env.getProperty("mybatis.mapperLocations");
|
||||||
aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
|
String configLocation = env.getProperty("mybatis.configLocation");
|
||||||
+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/"
|
typeAliasesPackage = MybatisUtils.setTypeAliasesPackage(typeAliasesPackage);
|
||||||
+ DEFAULT_RESOURCE_PATTERN;
|
VFS.addImplClass(SpringBootVFS.class);
|
||||||
Resource[] resources = resolver.getResources(aliasesPackage);
|
|
||||||
if (resources != null && resources.length > 0) {
|
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
|
||||||
MetadataReader metadataReader = null;
|
sessionFactory.setDataSource(dataSource);
|
||||||
for (Resource resource : resources) {
|
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
|
||||||
if (resource.isReadable()) {
|
sessionFactory.setMapperLocations(
|
||||||
metadataReader = metadataReaderFactory.getMetadataReader(resource);
|
MybatisUtils.resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
|
||||||
try {
|
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||||
result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage()
|
return sessionFactory.getObject();
|
||||||
.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 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
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()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SqlSessionFactory createSqlSessionFactory(Environment env, 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(name = "sqlSessionFactoryMaster")
|
|
||||||
@Primary
|
|
||||||
public SqlSessionFactory sqlSessionFactoryMaster(Environment env,
|
|
||||||
@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
|
|
||||||
return createSqlSessionFactory(env, dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(name = "sqlSessionFactorySlave")
|
|
||||||
@ConditionalOnBean(name = "slaveDataSource")
|
|
||||||
public SqlSessionFactory sqlSessionFactorySlave(Environment env,
|
|
||||||
@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
|
|
||||||
return createSqlSessionFactory(env, dataSource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "sqlSessionTemplate")
|
@Bean(name = "sqlSessionTemplate")
|
||||||
@ -155,4 +77,5 @@ public class MyBatisConfig {
|
|||||||
logger.error("Failed to register a SqlSessionFactory:{}", sqlSessionFactoryName);
|
logger.error("Failed to register a SqlSessionFactory:{}", sqlSessionFactoryName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.ruoyi.framework.config;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import com.ruoyi.common.interceptor.mybatis.CreateSqlSessionFactory;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SqlSessionFactoryConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CreateSqlSessionFactory createSqlSessionFactory;
|
||||||
|
|
||||||
|
@Bean(name = "sqlSessionFactoryMaster")
|
||||||
|
@Primary
|
||||||
|
public SqlSessionFactory sqlSessionFactoryMaster(Environment env,
|
||||||
|
@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
|
||||||
|
return createSqlSessionFactory.createSqlSessionFactory(env, dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "sqlSessionFactorySlave")
|
||||||
|
@ConditionalOnBean(name = "slaveDataSource")
|
||||||
|
public SqlSessionFactory sqlSessionFactorySlave(Environment env,
|
||||||
|
@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
|
||||||
|
return createSqlSessionFactory.createSqlSessionFactory(env, dataSource);
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- mybatis-plus 增强CRUD -->
|
<!-- mybatis-plus 增强CRUD -->
|
||||||
<dependency>
|
<!-- <dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
@ -39,6 +39,11 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
<version>${mybatis-plus-boot-starter.version}</version>
|
<version>${mybatis-plus-boot-starter.version}</version>
|
||||||
|
</dependency> -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>3.5.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- ruoyi-ehcache-->
|
<!-- ruoyi-ehcache-->
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
<!-- mybatis-plus 增强CRUD -->
|
<!-- mybatis-plus 增强CRUD -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
<!-- mybatis-plus 增强CRUD -->
|
<!-- mybatis-plus 增强CRUD -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -1,14 +1,27 @@
|
|||||||
package com.ruoyi.mybatisplus.config;
|
package com.ruoyi.mybatisplus.config;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.ibatis.io.VFS;
|
||||||
|
import org.apache.ibatis.plugin.Interceptor;
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
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.io.DefaultResourceLoader;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.DbType;
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
|
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
||||||
|
import com.ruoyi.common.interceptor.mybatis.CreateSqlSessionFactory;
|
||||||
|
import com.ruoyi.common.utils.MybatisUtils;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mybatis Plus 配置
|
* Mybatis Plus 配置
|
||||||
@ -17,11 +30,9 @@ import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerIntercept
|
|||||||
*/
|
*/
|
||||||
@EnableTransactionManagement(proxyTargetClass = true)
|
@EnableTransactionManagement(proxyTargetClass = true)
|
||||||
@Configuration
|
@Configuration
|
||||||
public class MybatisPlusConfig
|
public class MybatisPlusConfig {
|
||||||
{
|
|
||||||
@Bean
|
@Bean
|
||||||
public MybatisPlusInterceptor mybatisPlusInterceptor()
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
{
|
|
||||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
// 分页插件
|
// 分页插件
|
||||||
interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
||||||
@ -35,8 +46,7 @@ public class MybatisPlusConfig
|
|||||||
/**
|
/**
|
||||||
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
|
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
|
||||||
*/
|
*/
|
||||||
public PaginationInnerInterceptor paginationInnerInterceptor()
|
public PaginationInnerInterceptor paginationInnerInterceptor() {
|
||||||
{
|
|
||||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
||||||
// 设置数据库类型为mysql
|
// 设置数据库类型为mysql
|
||||||
paginationInnerInterceptor.setDbType(DbType.MYSQL);
|
paginationInnerInterceptor.setDbType(DbType.MYSQL);
|
||||||
@ -48,16 +58,39 @@ public class MybatisPlusConfig
|
|||||||
/**
|
/**
|
||||||
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
|
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
|
||||||
*/
|
*/
|
||||||
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
|
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
|
||||||
{
|
|
||||||
return new OptimisticLockerInnerInterceptor();
|
return new OptimisticLockerInnerInterceptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
|
* 如果是对全表的删除或更新操作,就会终止该操作
|
||||||
|
* https://baomidou.com/guide/interceptor-block-attack.html
|
||||||
*/
|
*/
|
||||||
public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
|
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
|
||||||
{
|
|
||||||
return new BlockAttackInnerInterceptor();
|
return new BlockAttackInnerInterceptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(prefix = "createSqlSessionFactory", name = "use", havingValue = "mybatis-plus")
|
||||||
|
public CreateSqlSessionFactory createSqlSessionFactory() {
|
||||||
|
return new CreateSqlSessionFactory() {
|
||||||
|
public SqlSessionFactory createSqlSessionFactory(Environment env, DataSource dataSource) throws Exception {
|
||||||
|
String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
|
||||||
|
String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
|
||||||
|
String configLocation = env.getProperty("mybatis-plus.configLocation");
|
||||||
|
typeAliasesPackage = MybatisUtils.setTypeAliasesPackage(typeAliasesPackage);
|
||||||
|
VFS.addImplClass(SpringBootVFS.class);
|
||||||
|
|
||||||
|
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
|
||||||
|
sessionFactory.setPlugins(new Interceptor[] { mybatisPlusInterceptor() });
|
||||||
|
sessionFactory.setDataSource(dataSource);
|
||||||
|
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
|
||||||
|
sessionFactory.setMapperLocations(
|
||||||
|
MybatisUtils.resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
|
||||||
|
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||||
|
return sessionFactory.getObject();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,107 @@
|
|||||||
|
package com.ruoyi.mybatisplus.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.ruoyi.common.annotation.Log;
|
||||||
|
import com.ruoyi.common.core.controller.BaseController;
|
||||||
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
|
import com.ruoyi.common.core.page.TableDataInfo;
|
||||||
|
import com.ruoyi.common.enums.BusinessType;
|
||||||
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||||
|
import com.ruoyi.mybatisplus.domain.SysStudent;
|
||||||
|
import com.ruoyi.mybatisplus.service.ISysStudentService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 学生信息Controller
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/system/student")
|
||||||
|
public class SysStudentController extends BaseController
|
||||||
|
{
|
||||||
|
@Autowired
|
||||||
|
private ISysStudentService sysStudentService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询学生信息列表
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:list')")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public TableDataInfo list(SysStudent sysStudent)
|
||||||
|
{
|
||||||
|
Page<SysStudent> page = new Page<SysStudent>(1,2);
|
||||||
|
List<SysStudent> list = sysStudentService.list(page);
|
||||||
|
return getDataTable(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出学生信息列表
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:export')")
|
||||||
|
@Log(title = "学生信息", businessType = BusinessType.EXPORT)
|
||||||
|
@GetMapping("/export")
|
||||||
|
public AjaxResult export(SysStudent sysStudent)
|
||||||
|
{
|
||||||
|
List<SysStudent> list = sysStudentService.queryList(sysStudent);
|
||||||
|
ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
|
||||||
|
return util.exportExcel(list, "student");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取学生信息详细信息
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:query')")
|
||||||
|
@GetMapping(value = "/{studentId}")
|
||||||
|
public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
|
||||||
|
{
|
||||||
|
return AjaxResult.success(sysStudentService.getById(studentId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增学生信息
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:add')")
|
||||||
|
@Log(title = "学生信息", businessType = BusinessType.INSERT)
|
||||||
|
@PostMapping
|
||||||
|
public AjaxResult add(@RequestBody SysStudent sysStudent)
|
||||||
|
{
|
||||||
|
return toAjax(sysStudentService.save(sysStudent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改学生信息
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:edit')")
|
||||||
|
@Log(title = "学生信息", businessType = BusinessType.UPDATE)
|
||||||
|
@PutMapping
|
||||||
|
public AjaxResult edit(@RequestBody SysStudent sysStudent)
|
||||||
|
{
|
||||||
|
return toAjax(sysStudentService.updateById(sysStudent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除学生信息
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('system:student:remove')")
|
||||||
|
@Log(title = "学生信息", businessType = BusinessType.DELETE)
|
||||||
|
@DeleteMapping("/{studentIds}")
|
||||||
|
public AjaxResult remove(@PathVariable Long[] studentIds)
|
||||||
|
{
|
||||||
|
return toAjax(sysStudentService.removeByIds(Arrays.asList(studentIds)));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
package com.ruoyi.mybatisplus.domain;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.ruoyi.common.annotation.Excel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 学生信息对象 sys_student
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@TableName(value = "sys_student")
|
||||||
|
public class SysStudent implements Serializable
|
||||||
|
{
|
||||||
|
@TableField(exist = false)
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/** 编号 */
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long studentId;
|
||||||
|
|
||||||
|
/** 学生名称 */
|
||||||
|
@Excel(name = "学生名称")
|
||||||
|
private String studentName;
|
||||||
|
|
||||||
|
/** 年龄 */
|
||||||
|
@Excel(name = "年龄")
|
||||||
|
private Integer studentAge;
|
||||||
|
|
||||||
|
/** 爱好(0代码 1音乐 2电影) */
|
||||||
|
@Excel(name = "爱好", readConverterExp = "0=代码,1=音乐,2=电影")
|
||||||
|
private String studentHobby;
|
||||||
|
|
||||||
|
/** 性别(0男 1女 2未知) */
|
||||||
|
@Excel(name = "性别", readConverterExp = "0=男,1=女,2=未知")
|
||||||
|
private String studentSex;
|
||||||
|
|
||||||
|
/** 状态(0正常 1停用) */
|
||||||
|
@Excel(name = "状态", readConverterExp = "0=正常,1=停用")
|
||||||
|
private String studentStatus;
|
||||||
|
|
||||||
|
/** 生日 */
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||||
|
@Excel(name = "生日", width = 30, dateFormat = "yyyy-MM-dd")
|
||||||
|
private Date studentBirthday;
|
||||||
|
|
||||||
|
public void setStudentId(Long studentId)
|
||||||
|
{
|
||||||
|
this.studentId = studentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getStudentId()
|
||||||
|
{
|
||||||
|
return studentId;
|
||||||
|
}
|
||||||
|
public void setStudentName(String studentName)
|
||||||
|
{
|
||||||
|
this.studentName = studentName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStudentName()
|
||||||
|
{
|
||||||
|
return studentName;
|
||||||
|
}
|
||||||
|
public void setStudentAge(Integer studentAge)
|
||||||
|
{
|
||||||
|
this.studentAge = studentAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getStudentAge()
|
||||||
|
{
|
||||||
|
return studentAge;
|
||||||
|
}
|
||||||
|
public void setStudentHobby(String studentHobby)
|
||||||
|
{
|
||||||
|
this.studentHobby = studentHobby;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStudentHobby()
|
||||||
|
{
|
||||||
|
return studentHobby;
|
||||||
|
}
|
||||||
|
public void setStudentSex(String studentSex)
|
||||||
|
{
|
||||||
|
this.studentSex = studentSex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStudentSex()
|
||||||
|
{
|
||||||
|
return studentSex;
|
||||||
|
}
|
||||||
|
public void setStudentStatus(String studentStatus)
|
||||||
|
{
|
||||||
|
this.studentStatus = studentStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStudentStatus()
|
||||||
|
{
|
||||||
|
return studentStatus;
|
||||||
|
}
|
||||||
|
public void setStudentBirthday(Date studentBirthday)
|
||||||
|
{
|
||||||
|
this.studentBirthday = studentBirthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getStudentBirthday()
|
||||||
|
{
|
||||||
|
return studentBirthday;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||||
|
.append("studentId", getStudentId())
|
||||||
|
.append("studentName", getStudentName())
|
||||||
|
.append("studentAge", getStudentAge())
|
||||||
|
.append("studentHobby", getStudentHobby())
|
||||||
|
.append("studentSex", getStudentSex())
|
||||||
|
.append("studentStatus", getStudentStatus())
|
||||||
|
.append("studentBirthday", getStudentBirthday())
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.ruoyi.mybatisplus.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.ruoyi.mybatisplus.domain.SysStudent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 学生信息Mapper接口
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public interface SysStudentMapper extends BaseMapper<SysStudent>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.ruoyi.mybatisplus.service;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.ruoyi.mybatisplus.domain.SysStudent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 学生信息Service接口
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public interface ISysStudentService extends IService<SysStudent>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 查询学生信息列表
|
||||||
|
*
|
||||||
|
* @param sysStudent 学生信息
|
||||||
|
* @return 学生信息集合
|
||||||
|
*/
|
||||||
|
public List<SysStudent> queryList(SysStudent sysStudent);
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.ruoyi.mybatisplus.service.impl;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.mybatisplus.domain.SysStudent;
|
||||||
|
import com.ruoyi.mybatisplus.mapper.SysStudentMapper;
|
||||||
|
import com.ruoyi.mybatisplus.service.ISysStudentService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 学生信息Service业务层处理
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SysStudentServiceImpl extends ServiceImpl<SysStudentMapper, SysStudent> implements ISysStudentService
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public List<SysStudent> queryList(SysStudent sysStudent)
|
||||||
|
{
|
||||||
|
// 注意:mybatis-plus lambda 模式不支持 eclipse 的编译器
|
||||||
|
// LambdaQueryWrapper<SysStudent> queryWrapper = Wrappers.lambdaQuery();
|
||||||
|
// queryWrapper.eq(SysStudent::getStudentName, sysStudent.getStudentName());
|
||||||
|
QueryWrapper<SysStudent> queryWrapper = Wrappers.query();
|
||||||
|
if (StringUtils.isNotEmpty(sysStudent.getStudentName()))
|
||||||
|
{
|
||||||
|
queryWrapper.eq("student_name", sysStudent.getStudentName());
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotNull(sysStudent.getStudentAge()))
|
||||||
|
{
|
||||||
|
queryWrapper.eq("student_age", sysStudent.getStudentAge());
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotEmpty(sysStudent.getStudentHobby()))
|
||||||
|
{
|
||||||
|
queryWrapper.eq("student_hobby", sysStudent.getStudentHobby());
|
||||||
|
}
|
||||||
|
return this.list(queryWrapper);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user