forked from ruoyi/RuoYi-Vue
添加mybatis-plus增强
This commit is contained in:
@ -98,8 +98,8 @@ token:
|
|||||||
# 令牌有效期(默认30分钟)
|
# 令牌有效期(默认30分钟)
|
||||||
expireTime: 30
|
expireTime: 30
|
||||||
|
|
||||||
# MyBatis配置
|
# MyBatis Plus配置
|
||||||
mybatis:
|
mybatis-plus:
|
||||||
# 搜索指定包别名
|
# 搜索指定包别名
|
||||||
typeAliasesPackage: com.devttl.**.domain
|
typeAliasesPackage: com.devttl.**.domain
|
||||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||||
|
@ -119,6 +119,21 @@
|
|||||||
<artifactId>jakarta.servlet-api</artifactId>
|
<artifactId>jakarta.servlet-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,132 +0,0 @@
|
|||||||
package com.devttl.framework.config;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
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;
|
|
||||||
import com.devttl.common.utils.StringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mybatis支持*匹配扫描包
|
|
||||||
*
|
|
||||||
* @author devttl
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class MyBatisConfig
|
|
||||||
{
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
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 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
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.devttl.framework.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mybatis Plus 配置
|
||||||
|
*
|
||||||
|
* @author devttl
|
||||||
|
*/
|
||||||
|
@EnableTransactionManagement(proxyTargetClass = true)
|
||||||
|
@Configuration
|
||||||
|
public class MybatisPlusConfig {
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
|
// 分页插件
|
||||||
|
interceptor.addInnerInterceptor(paginationInnerInterceptor());
|
||||||
|
// 乐观锁插件
|
||||||
|
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
||||||
|
// 阻断插件
|
||||||
|
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页插件,自动识别数据库类型
|
||||||
|
* https://baomidou.com/guide/interceptor-pagination.html
|
||||||
|
*/
|
||||||
|
public PaginationInnerInterceptor paginationInnerInterceptor() {
|
||||||
|
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
||||||
|
// 设置数据库类型为mysql
|
||||||
|
paginationInnerInterceptor.setDbType(DbType.MYSQL);
|
||||||
|
// 设置最大单页限制数量,默认 500 条,-1 不受限制
|
||||||
|
paginationInnerInterceptor.setMaxLimit(-1L);
|
||||||
|
return paginationInnerInterceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 乐观锁插件
|
||||||
|
* https://baomidou.com/guide/interceptor-optimistic-locker.html
|
||||||
|
*/
|
||||||
|
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
|
||||||
|
return new OptimisticLockerInnerInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果是对全表的删除或更新操作,就会终止该操作
|
||||||
|
* https://baomidou.com/guide/interceptor-block-attack.html
|
||||||
|
*/
|
||||||
|
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
|
||||||
|
return new BlockAttackInnerInterceptor();
|
||||||
|
}
|
||||||
|
}
|
20
pom.xml
20
pom.xml
@ -17,6 +17,9 @@
|
|||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
||||||
|
<mybatis.version>3.5.16</mybatis.version>
|
||||||
|
<mybatis-plus.version>3.5.12</mybatis-plus.version>
|
||||||
|
<mybatis-plus-jsqlparser.version>3.5.12</mybatis-plus-jsqlparser.version>
|
||||||
<mybatis-spring-boot.version>3.0.3</mybatis-spring-boot.version>
|
<mybatis-spring-boot.version>3.0.3</mybatis-spring-boot.version>
|
||||||
<druid.version>1.2.23</druid.version>
|
<druid.version>1.2.23</druid.version>
|
||||||
<bitwalker.version>1.21</bitwalker.version>
|
<bitwalker.version>1.21</bitwalker.version>
|
||||||
@ -184,6 +187,23 @@
|
|||||||
<version>${devttl.version}</version>
|
<version>${devttl.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
<version>${mybatis.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>${mybatis-plus.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
|
<version>${mybatis-plus-jsqlparser.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user