mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-16 21:33:40 +00:00
v3.0.0 init
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
package org.ruoyi.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.ruoyi.annotation.DataPermission;
|
||||
import org.ruoyi.helper.DataPermissionHelper;
|
||||
|
||||
/**
|
||||
* 数据权限处理
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
public class DataPermissionAspect {
|
||||
|
||||
/**
|
||||
* 处理请求前执行
|
||||
*/
|
||||
@Before(value = "@annotation(dataPermission)")
|
||||
public void doBefore(JoinPoint joinPoint, DataPermission dataPermission) {
|
||||
DataPermissionHelper.setPermission(dataPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理完请求后执行
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
*/
|
||||
@AfterReturning(pointcut = "@annotation(dataPermission)")
|
||||
public void doAfterReturning(JoinPoint joinPoint, DataPermission dataPermission) {
|
||||
DataPermissionHelper.removePermission();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截异常操作
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
* @param e 异常
|
||||
*/
|
||||
@AfterThrowing(value = "@annotation(dataPermission)", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, DataPermission dataPermission, Exception e) {
|
||||
DataPermissionHelper.removePermission();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.annotation;
|
||||
package org.ruoyi.common.mybatis.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.annotation;
|
||||
package org.ruoyi.common.mybatis.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.ruoyi.common.mybatis.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.ruoyi.common.mybatis.annotation.DataPermission;
|
||||
import org.ruoyi.common.mybatis.helper.DataPermissionHelper;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* 数据权限注解Advice
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Slf4j
|
||||
public class DataPermissionAdvice implements MethodInterceptor {
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Object target = invocation.getThis();
|
||||
Method method = invocation.getMethod();
|
||||
Object[] args = invocation.getArguments();
|
||||
// 设置权限注解
|
||||
DataPermissionHelper.setPermission(getDataPermissionAnnotation(target, method, args));
|
||||
try {
|
||||
// 执行代理方法
|
||||
return invocation.proceed();
|
||||
} finally {
|
||||
// 清除权限注解
|
||||
DataPermissionHelper.removePermission();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据权限注解
|
||||
*/
|
||||
private DataPermission getDataPermissionAnnotation(Object target, Method method,Object[] args){
|
||||
DataPermission dataPermission = method.getAnnotation(DataPermission.class);
|
||||
// 优先获取方法上的注解
|
||||
if (dataPermission != null) {
|
||||
return dataPermission;
|
||||
}
|
||||
// 方法上没有注解,则获取类上的注解
|
||||
Class<?> targetClass = target.getClass();
|
||||
// 如果是 JDK 动态代理,则获取真实的Class实例
|
||||
if (Proxy.isProxyClass(targetClass)) {
|
||||
targetClass = targetClass.getInterfaces()[0];
|
||||
}
|
||||
dataPermission = targetClass.getAnnotation(DataPermission.class);
|
||||
return dataPermission;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.ruoyi.common.mybatis.aspect;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.mybatis.annotation.DataPermission;
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcut;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* 数据权限匹配切点
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@Slf4j
|
||||
@SuppressWarnings("all")
|
||||
public class DataPermissionPointcut extends StaticMethodMatcherPointcut {
|
||||
|
||||
@Override
|
||||
public boolean matches(Method method, Class<?> targetClass) {
|
||||
// 优先匹配方法
|
||||
// 数据权限注解不对继承生效,所以检查当前方法是否有注解即可,不再往上匹配父类或接口
|
||||
if (method.isAnnotationPresent(DataPermission.class)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// MyBatis 的 Mapper 就是通过 JDK 动态代理实现的,所以这里需要检查是否匹配 JDK 的动态代理
|
||||
Class<?> targetClassRef = targetClass;
|
||||
if (Proxy.isProxyClass(targetClassRef)) {
|
||||
// 数据权限注解不对继承生效,但由于 SpringIOC 容器拿到的实际上是 MyBatis 代理过后的 Mapper,而 targetClass.isAnnotationPresent 实际匹配的是 Proxy 类的注解,不会查找代理类。
|
||||
// 所以这里不能用 targetClass.isAnnotationPresent,只能用 AnnotatedElementUtils.hasAnnotation 或 targetClass.getInterfaces()[0].isAnnotationPresent 去做匹配,以检查被代理的 MapperClass 是否具有注解
|
||||
// 原理:JDK 动态代理本质上就是对接口进行实现然后对具体的接口实现做代理,所以直接通过接口可以拿到实际的 MapperClass
|
||||
targetClassRef = targetClass.getInterfaces()[0];
|
||||
|
||||
}
|
||||
return targetClassRef.isAnnotationPresent(DataPermission.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.ruoyi.common.mybatis.aspect;
|
||||
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.springframework.aop.Pointcut;
|
||||
import org.springframework.aop.support.AbstractPointcutAdvisor;
|
||||
|
||||
/**
|
||||
* 数据权限注解切面定义
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class DataPermissionPointcutAdvisor extends AbstractPointcutAdvisor {
|
||||
|
||||
private final Advice advice;
|
||||
private final Pointcut pointcut;
|
||||
|
||||
public DataPermissionPointcutAdvisor() {
|
||||
this.advice = new DataPermissionAdvice();
|
||||
this.pointcut = new DataPermissionPointcut();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pointcut getPointcut() {
|
||||
return this.pointcut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Advice getAdvice() {
|
||||
return this.advice;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.config;
|
||||
package org.ruoyi.common.mybatis.config;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
@@ -9,17 +9,19 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.ruoyi.aspect.DataPermissionAspect;
|
||||
import org.ruoyi.common.core.factory.YmlPropertySourceFactory;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
import org.ruoyi.handler.InjectionMetaObjectHandler;
|
||||
import org.ruoyi.handler.MybatisExceptionHandler;
|
||||
import org.ruoyi.handler.PlusPostInitTableInfoHandler;
|
||||
import org.ruoyi.interceptor.PlusDataPermissionInterceptor;
|
||||
import org.ruoyi.common.mybatis.aspect.DataPermissionPointcutAdvisor;
|
||||
import org.ruoyi.common.mybatis.handler.InjectionMetaObjectHandler;
|
||||
import org.ruoyi.common.mybatis.handler.MybatisExceptionHandler;
|
||||
import org.ruoyi.common.mybatis.handler.PlusPostInitTableInfoHandler;
|
||||
import org.ruoyi.common.mybatis.interceptor.PlusDataPermissionInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.annotation.Role;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
@@ -27,6 +29,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
@EnableTransactionManagement(proxyTargetClass = true)
|
||||
@MapperScan("${mybatis-plus.mapperPackage}")
|
||||
@PropertySource(value = "classpath:common-mybatis.yml", factory = YmlPropertySourceFactory.class)
|
||||
@@ -54,15 +57,16 @@ public class MybatisPlusConfig {
|
||||
* 数据权限拦截器
|
||||
*/
|
||||
public PlusDataPermissionInterceptor dataPermissionInterceptor() {
|
||||
return new PlusDataPermissionInterceptor(SpringUtils.getProperty("mybatis-plus.mapperPackage"));
|
||||
return new PlusDataPermissionInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限切面处理器
|
||||
*/
|
||||
@Bean
|
||||
public DataPermissionAspect dataPermissionAspect() {
|
||||
return new DataPermissionAspect();
|
||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
public DataPermissionPointcutAdvisor dataPermissionPointcutAdvisor() {
|
||||
return new DataPermissionPointcutAdvisor();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.core.domain;
|
||||
package org.ruoyi.common.mybatis.core.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.core.mapper;
|
||||
package org.ruoyi.common.mybatis.core.mapper;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.core.page;
|
||||
package org.ruoyi.common.mybatis.core.page;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -23,37 +23,38 @@ import java.util.List;
|
||||
@Data
|
||||
public class PageQuery implements Serializable {
|
||||
|
||||
/**
|
||||
* 当前记录起始索引 默认值
|
||||
*/
|
||||
public static final int DEFAULT_PAGE_NUM = 1;
|
||||
/**
|
||||
* 每页显示记录数 默认值 默认查全部
|
||||
*/
|
||||
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
private Integer pageSize;
|
||||
|
||||
/**
|
||||
* 当前页数
|
||||
*/
|
||||
private Integer pageNum;
|
||||
|
||||
/**
|
||||
* 排序列
|
||||
*/
|
||||
private String orderByColumn;
|
||||
|
||||
/**
|
||||
* 排序的方向desc或者asc
|
||||
*/
|
||||
private String isAsc;
|
||||
|
||||
public PageQuery(Integer pageSize, Integer pageNum) {
|
||||
this.pageSize = pageSize;
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
/**
|
||||
* 当前记录起始索引 默认值
|
||||
*/
|
||||
public static final int DEFAULT_PAGE_NUM = 1;
|
||||
|
||||
/**
|
||||
* 每页显示记录数 默认值 默认查全部
|
||||
*/
|
||||
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* 构建分页对象
|
||||
@@ -74,7 +75,7 @@ public class PageQuery implements Serializable {
|
||||
|
||||
/**
|
||||
* 构建排序
|
||||
* <p>
|
||||
*
|
||||
* 支持的用法如下:
|
||||
* {isAsc:"asc",orderByColumn:"id"} order by id asc
|
||||
* {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
|
||||
@@ -118,4 +119,9 @@ public class PageQuery implements Serializable {
|
||||
return (pageNum - 1) * pageSize;
|
||||
}
|
||||
|
||||
public PageQuery(Integer pageSize, Integer pageNum) {
|
||||
this.pageSize = pageSize;
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.ruoyi.core.page;
|
||||
package org.ruoyi.common.mybatis.core.page;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.Data;
|
||||
@@ -88,4 +89,19 @@ public class TableDataInfo<T> implements Serializable {
|
||||
return rspData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始数据列表和分页参数,构建表格分页数据对象(用于假分页)
|
||||
*
|
||||
* @param list 原始数据列表(全部数据)
|
||||
* @param page 分页参数对象(包含当前页码、每页大小等)
|
||||
* @return 构造好的分页结果 TableDataInfo<T>
|
||||
*/
|
||||
public static <T> TableDataInfo<T> build(List<T> list, IPage<T> page) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return TableDataInfo.build();
|
||||
}
|
||||
List<T> pageList = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), list);
|
||||
return new TableDataInfo<>(pageList, list.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package org.ruoyi.enums;
|
||||
package org.ruoyi.common.mybatis.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 数据库类型
|
||||
*
|
||||
@@ -43,17 +42,46 @@ public enum DataBaseType {
|
||||
* 根据数据库产品名称查找对应的数据库类型
|
||||
*
|
||||
* @param databaseProductName 数据库产品名称
|
||||
* @return 对应的数据库类型枚举值,如果未找到则返回 null
|
||||
* @return 对应的数据库类型枚举值
|
||||
*/
|
||||
public static DataBaseType find(String databaseProductName) {
|
||||
if (StringUtils.isBlank(databaseProductName)) {
|
||||
return null;
|
||||
return MY_SQL;
|
||||
}
|
||||
for (DataBaseType type : values()) {
|
||||
if (type.getType().equals(databaseProductName)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return MY_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 MySQL 类型
|
||||
*/
|
||||
public boolean isMySql() {
|
||||
return this == MY_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 Oracle 类型
|
||||
*/
|
||||
public boolean isOracle() {
|
||||
return this == ORACLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 PostgreSQL 类型
|
||||
*/
|
||||
public boolean isPostgreSql() {
|
||||
return this == POSTGRE_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 SQL Server 类型
|
||||
*/
|
||||
public boolean isSqlServer() {
|
||||
return this == SQL_SERVER;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.ruoyi.enums;
|
||||
package org.ruoyi.common.mybatis.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.ruoyi.common.core.domain.model.LoginUser;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.helper.DataPermissionHelper;
|
||||
import org.ruoyi.common.mybatis.helper.DataPermissionHelper;
|
||||
|
||||
/**
|
||||
* 数据权限类型枚举
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.handler;
|
||||
package org.ruoyi.common.mybatis.handler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
@@ -7,10 +7,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.ruoyi.common.core.domain.model.LoginUser;
|
||||
import org.ruoyi.common.core.exception.ServiceException;
|
||||
import org.ruoyi.common.core.service.BaseContext;
|
||||
import org.ruoyi.common.core.utils.ObjectUtils;
|
||||
import org.ruoyi.common.mybatis.core.domain.BaseEntity;
|
||||
import org.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import org.ruoyi.core.domain.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -23,6 +22,11 @@ import java.util.Date;
|
||||
@Slf4j
|
||||
public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
/**
|
||||
* 如果用户不存在默认注入-1代表无用户
|
||||
*/
|
||||
private static final Long DEFAULT_USER_ID = -1L;
|
||||
|
||||
/**
|
||||
* 插入填充方法,用于在插入数据时自动填充实体对象中的创建时间、更新时间、创建人、更新人等信息
|
||||
*
|
||||
@@ -46,6 +50,11 @@ public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
baseEntity.setCreateBy(userId);
|
||||
baseEntity.setUpdateBy(userId);
|
||||
baseEntity.setCreateDept(ObjectUtils.notNull(baseEntity.getCreateDept(), loginUser.getDeptId()));
|
||||
} else {
|
||||
// 填充创建人、更新人和创建部门信息
|
||||
baseEntity.setCreateBy(DEFAULT_USER_ID);
|
||||
baseEntity.setUpdateBy(DEFAULT_USER_ID);
|
||||
baseEntity.setCreateDept(ObjectUtils.notNull(baseEntity.getCreateDept(), DEFAULT_USER_ID));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -75,6 +84,8 @@ public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
Long userId = LoginHelper.getUserId();
|
||||
if (ObjectUtil.isNotNull(userId)) {
|
||||
baseEntity.setUpdateBy(userId);
|
||||
} else {
|
||||
baseEntity.setUpdateBy(DEFAULT_USER_ID);
|
||||
}
|
||||
} else {
|
||||
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
|
||||
@@ -92,10 +103,8 @@ public class InjectionMetaObjectHandler implements MetaObjectHandler {
|
||||
private LoginUser getLoginUser() {
|
||||
LoginUser loginUser;
|
||||
try {
|
||||
String token = BaseContext.getCurrentToken();
|
||||
loginUser = LoginHelper.getLoginUser(token);
|
||||
loginUser = LoginHelper.getLoginUser();
|
||||
} catch (Exception e) {
|
||||
log.warn("自动注入警告 => 用户未登录");
|
||||
return null;
|
||||
}
|
||||
return loginUser;
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.ruoyi.common.mybatis.handler;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.core.domain.R;
|
||||
import org.mybatis.spring.MyBatisSystemException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* Mybatis异常处理器
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class MybatisExceptionHandler {
|
||||
|
||||
/**
|
||||
* 主键或UNIQUE索引,数据重复异常
|
||||
*/
|
||||
@ExceptionHandler(DuplicateKeyException.class)
|
||||
public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage());
|
||||
return R.fail(HttpStatus.HTTP_CONFLICT, "数据库中已存在该记录,请联系管理员确认");
|
||||
}
|
||||
|
||||
/**
|
||||
* Mybatis系统异常 通用处理
|
||||
*/
|
||||
@ExceptionHandler(MyBatisSystemException.class)
|
||||
public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
Throwable root = getRootCause(e);
|
||||
if (root instanceof NotLoginException) {
|
||||
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, root.getMessage());
|
||||
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
|
||||
}
|
||||
if (root instanceof CannotFindDataSourceException) {
|
||||
log.error("请求地址'{}', 未找到数据源", requestURI);
|
||||
return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "未找到数据源,请联系管理员确认");
|
||||
}
|
||||
log.error("请求地址'{}', Mybatis系统异常", requestURI, e);
|
||||
return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取异常的根因(递归查找)
|
||||
*
|
||||
* @param e 当前异常
|
||||
* @return 根因异常(最底层的 cause)
|
||||
* <p>
|
||||
* 逻辑说明:
|
||||
* 1. 如果 e 没有 cause,说明 e 本身就是根因,直接返回
|
||||
* 2. 如果 e 的 cause 和自身相同(防止循环引用),也返回 e
|
||||
* 3. 否则递归调用,继续向下寻找最底层的 cause
|
||||
*/
|
||||
public static Throwable getRootCause(Throwable e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause == null || cause == e) {
|
||||
return e;
|
||||
}
|
||||
return getRootCause(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在异常链中查找指定类型的异常
|
||||
*
|
||||
* @param e 当前异常
|
||||
* @param clazz 目标异常类
|
||||
* @return 找到的指定类型异常,如果没有找到返回 null
|
||||
*/
|
||||
public static Throwable findCause(Throwable e, Class<? extends Throwable> clazz) {
|
||||
Throwable t = e;
|
||||
while (t != null && t != t.getCause()) {
|
||||
if (clazz.isInstance(t)) {
|
||||
return t;
|
||||
}
|
||||
t = t.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.ruoyi.handler;
|
||||
package org.ruoyi.common.mybatis.handler;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -10,34 +9,24 @@ import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.ruoyi.annotation.DataColumn;
|
||||
import org.ruoyi.annotation.DataPermission;
|
||||
import org.ruoyi.common.core.domain.dto.RoleDTO;
|
||||
import org.ruoyi.common.core.domain.model.LoginUser;
|
||||
import org.ruoyi.common.core.exception.ServiceException;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
import org.ruoyi.common.core.utils.StreamUtils;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.mybatis.annotation.DataColumn;
|
||||
import org.ruoyi.common.mybatis.annotation.DataPermission;
|
||||
import org.ruoyi.common.mybatis.enums.DataScopeType;
|
||||
import org.ruoyi.common.mybatis.helper.DataPermissionHelper;
|
||||
import org.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import org.ruoyi.enums.DataScopeType;
|
||||
import org.ruoyi.helper.DataPermissionHelper;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.expression.BeanFactoryResolver;
|
||||
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.ClassMetadata;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
import org.springframework.expression.*;
|
||||
import org.springframework.expression.common.TemplateParserContext;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -49,11 +38,6 @@ import java.util.function.Function;
|
||||
@Slf4j
|
||||
public class PlusDataPermissionHandler {
|
||||
|
||||
/**
|
||||
* 类名称与注解的映射关系缓存(由于aop无法拦截mybatis接口类上的注解 只能通过启动预扫描的方式进行)
|
||||
*/
|
||||
private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* spel 解析器
|
||||
*/
|
||||
@@ -64,27 +48,17 @@ public class PlusDataPermissionHandler {
|
||||
*/
|
||||
private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory());
|
||||
|
||||
/**
|
||||
* 构造方法,扫描指定包下的 Mapper 类并初始化缓存
|
||||
*
|
||||
* @param mapperPackage Mapper 类所在的包路径
|
||||
*/
|
||||
public PlusDataPermissionHandler(String mapperPackage) {
|
||||
scanMapperClasses(mapperPackage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据过滤条件的 SQL 片段
|
||||
*
|
||||
* @param where 原始的查询条件表达式
|
||||
* @param mappedStatementId Mapper 方法的 ID
|
||||
* @param isSelect 是否为查询语句
|
||||
* @return 数据过滤条件的 SQL 片段
|
||||
*/
|
||||
public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) {
|
||||
public Expression getSqlSegment(Expression where, boolean isSelect) {
|
||||
try {
|
||||
// 获取数据权限配置
|
||||
DataPermission dataPermission = getDataPermission(mappedStatementId);
|
||||
DataPermission dataPermission = getDataPermission();
|
||||
// 获取当前登录用户信息
|
||||
LoginUser currentUser = DataPermissionHelper.getVariable("user");
|
||||
if (ObjectUtil.isNull(currentUser)) {
|
||||
@@ -145,7 +119,7 @@ public class PlusDataPermissionHandler {
|
||||
}
|
||||
// 包含权限标识符 这直接跳过
|
||||
if (StringUtils.isNotBlank(dataColumn.permission()) &&
|
||||
CollUtil.contains(user.getMenuPermission(), dataColumn.permission())
|
||||
CollUtil.contains(user.getMenuPermission(), dataColumn.permission())
|
||||
) {
|
||||
ignoreMap.put(dataColumn, Boolean.TRUE);
|
||||
continue;
|
||||
@@ -172,7 +146,6 @@ public class PlusDataPermissionHandler {
|
||||
for (DataColumn dataColumn : dataPermission.value()) {
|
||||
// 包含权限标识符 这直接跳过
|
||||
if (ignoreMap.containsKey(dataColumn)) {
|
||||
// 修复多角色与权限标识符共用问题 https://gitee.com/dromara/RuoYi-Vue-Plus/issues/IB4CS4
|
||||
conditions.add(joinStr + " 1 = 1 ");
|
||||
isSuccess = true;
|
||||
continue;
|
||||
@@ -187,7 +160,7 @@ public class PlusDataPermissionHandler {
|
||||
}
|
||||
// 忽略数据权限 防止spel表达式内有其他sql查询导致死循环调用
|
||||
String sql = DataPermissionHelper.ignore(() ->
|
||||
parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class)
|
||||
parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class)
|
||||
);
|
||||
// 解析sql模板并填充
|
||||
conditions.add(joinStr + sql);
|
||||
@@ -206,92 +179,22 @@ public class PlusDataPermissionHandler {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描指定包下的 Mapper 类,并查找其中带有特定注解的方法或类
|
||||
*
|
||||
* @param mapperPackage Mapper 类所在的包路径
|
||||
*/
|
||||
private void scanMapperClasses(String mapperPackage) {
|
||||
// 创建资源解析器和元数据读取工厂
|
||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
|
||||
// 将 Mapper 包路径按分隔符拆分为数组
|
||||
String[] packagePatternArray = StringUtils.splitPreserveAllTokens(mapperPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
|
||||
String classpath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX;
|
||||
try {
|
||||
for (String packagePattern : packagePatternArray) {
|
||||
// 将包路径转换为资源路径
|
||||
String path = ClassUtils.convertClassNameToResourcePath(packagePattern);
|
||||
// 获取指定路径下的所有 .class 文件资源
|
||||
Resource[] resources = resolver.getResources(classpath + path + "/*.class");
|
||||
for (Resource resource : resources) {
|
||||
// 获取资源的类元数据
|
||||
ClassMetadata classMetadata = factory.getMetadataReader(resource).getClassMetadata();
|
||||
// 获取资源对应的类对象
|
||||
Class<?> clazz = Resources.classForName(classMetadata.getClassName());
|
||||
// 查找类中的特定注解
|
||||
findAnnotation(clazz);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("初始化数据安全缓存时出错:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在指定的类中查找特定的注解 DataPermission,并将带有这个注解的方法或类存储到 dataPermissionCacheMap 中
|
||||
*
|
||||
* @param clazz 要查找的类
|
||||
*/
|
||||
private void findAnnotation(Class<?> clazz) {
|
||||
DataPermission dataPermission;
|
||||
for (Method method : clazz.getMethods()) {
|
||||
if (method.isDefault() || method.isVarArgs()) {
|
||||
continue;
|
||||
}
|
||||
String mappedStatementId = clazz.getName() + "." + method.getName();
|
||||
if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) {
|
||||
dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class);
|
||||
dataPermissionCacheMap.put(mappedStatementId, dataPermission);
|
||||
}
|
||||
}
|
||||
if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) {
|
||||
dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class);
|
||||
dataPermissionCacheMap.put(clazz.getName(), dataPermission);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据映射语句 ID 或类名获取对应的 DataPermission 注解对象
|
||||
*
|
||||
* @param mapperId 映射语句 ID
|
||||
* @return DataPermission 注解对象,如果不存在则返回 null
|
||||
*/
|
||||
public DataPermission getDataPermission(String mapperId) {
|
||||
// 检查上下文中是否包含映射语句 ID 对应的 DataPermission 注解对象
|
||||
if (DataPermissionHelper.getPermission() != null) {
|
||||
return DataPermissionHelper.getPermission();
|
||||
}
|
||||
// 检查缓存中是否包含映射语句 ID 对应的 DataPermission 注解对象
|
||||
if (dataPermissionCacheMap.containsKey(mapperId)) {
|
||||
return dataPermissionCacheMap.get(mapperId);
|
||||
}
|
||||
// 如果缓存中不包含映射语句 ID 对应的 DataPermission 注解对象,则尝试使用类名作为键查找
|
||||
String clazzName = mapperId.substring(0, mapperId.lastIndexOf("."));
|
||||
if (dataPermissionCacheMap.containsKey(clazzName)) {
|
||||
return dataPermissionCacheMap.get(clazzName);
|
||||
}
|
||||
return null;
|
||||
public DataPermission getDataPermission() {
|
||||
return DataPermissionHelper.getPermission();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查给定的映射语句 ID 是否有效,即是否能够找到对应的 DataPermission 注解对象
|
||||
*
|
||||
* @param mapperId 映射语句 ID
|
||||
* @return 如果找到对应的 DataPermission 注解对象,则返回 false;否则返回 true
|
||||
*/
|
||||
public boolean invalid(String mapperId) {
|
||||
return getDataPermission(mapperId) == null;
|
||||
public boolean invalid() {
|
||||
return getDataPermission() == null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.handler;
|
||||
package org.ruoyi.common.mybatis.handler;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.baomidou.mybatisplus.core.handlers.PostInitTableInfoHandler;
|
||||
@@ -7,7 +7,6 @@ import org.apache.ibatis.session.Configuration;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
import org.ruoyi.common.core.utils.reflect.ReflectUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 修改表信息初始化方式
|
||||
* 目前用于全局修改是否使用逻辑删除
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.ruoyi.common.mybatis.helper;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.ruoyi.common.core.exception.ServiceException;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
import org.ruoyi.common.mybatis.enums.DataBaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据库助手
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class DataBaseHelper {
|
||||
|
||||
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
||||
|
||||
/**
|
||||
* 获取当前数据源对应的数据库类型
|
||||
* <p>
|
||||
* 通过 DynamicRoutingDataSource 获取当前线程绑定的数据源,
|
||||
* 然后从数据源获取数据库连接,利用连接的元数据获取数据库产品名称,
|
||||
* 最后调用 DataBaseType.find 方法将数据库名称转换为对应的枚举类型
|
||||
*
|
||||
* @return 当前数据库对应的 DataBaseType 枚举,找不到时默认返回 MY_SQL
|
||||
* @throws ServiceException 当获取数据库连接或元数据出现异常时抛出业务异常
|
||||
*/
|
||||
public static DataBaseType getDataBaseType() {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String databaseProductName = metaData.getDatabaseProductName();
|
||||
return DataBaseType.find(databaseProductName);
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据当前数据库类型,生成兼容的 FIND_IN_SET 语句片段
|
||||
* <p>
|
||||
* 用于判断指定值是否存在于逗号分隔的字符串列中,SQL写法根据不同数据库方言自动切换:
|
||||
* - Oracle 使用 instr 函数
|
||||
* - PostgreSQL 使用 strpos 函数
|
||||
* - SQL Server 使用 charindex 函数
|
||||
* - 其他默认使用 MySQL 的 find_in_set 函数
|
||||
*
|
||||
* @param var1 要查找的值(支持任意类型,内部会转换成字符串)
|
||||
* @param var2 存储逗号分隔值的数据库列名
|
||||
* @return 适用于当前数据库的 SQL 条件字符串,通常用于 where 或 apply 中拼接
|
||||
*/
|
||||
public static String findInSet(Object var1, String var2) {
|
||||
String var = Convert.toStr(var1);
|
||||
return switch (getDataBaseType()) {
|
||||
// instr(',0,100,101,' , ',100,') <> 0
|
||||
case ORACLE -> "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var);
|
||||
// (select strpos(',0,100,101,' , ',100,')) <> 0
|
||||
case POSTGRE_SQL -> "(select strpos(','||%s||',' , ',%s,')) <> 0".formatted(var2, var);
|
||||
// charindex(',100,' , ',0,100,101,') <> 0
|
||||
case SQL_SERVER -> "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2);
|
||||
// find_in_set(100 , '0,100,101')
|
||||
default -> "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前加载的数据库名
|
||||
*/
|
||||
public static List<String> getDataSourceNameList() {
|
||||
return new ArrayList<>(DS.getDataSources().keySet());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.helper;
|
||||
package org.ruoyi.common.mybatis.helper;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@@ -8,8 +8,8 @@ import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.ruoyi.annotation.DataPermission;
|
||||
import org.ruoyi.common.core.utils.reflect.ReflectUtils;
|
||||
import org.ruoyi.common.mybatis.annotation.DataPermission;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -44,7 +44,7 @@ public class DataPermissionHelper {
|
||||
/**
|
||||
* 设置当前执行mapper权限注解
|
||||
*
|
||||
* @param dataPermission 数据权限注解
|
||||
* @param dataPermission 数据权限注解
|
||||
*/
|
||||
public static void setPermission(DataPermission dataPermission) {
|
||||
PERMISSION_CACHE.set(dataPermission);
|
||||
@@ -130,10 +130,10 @@ public class DataPermissionHelper {
|
||||
IgnoreStrategy ignoreStrategy = getIgnoreStrategy();
|
||||
if (ObjectUtil.isNotNull(ignoreStrategy)) {
|
||||
boolean noOtherIgnoreStrategy = !Boolean.TRUE.equals(ignoreStrategy.getDynamicTableName())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getTenantLine())
|
||||
&& CollectionUtil.isEmpty(ignoreStrategy.getOthers());
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getBlockAttack())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getIllegalSql())
|
||||
&& !Boolean.TRUE.equals(ignoreStrategy.getTenantLine())
|
||||
&& CollectionUtil.isEmpty(ignoreStrategy.getOthers());
|
||||
Stack<Integer> reentrantStack = REENTRANT_IGNORE.get();
|
||||
boolean empty = reentrantStack.isEmpty() || reentrantStack.pop() == 1;
|
||||
if (noOtherIgnoreStrategy && empty) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.ruoyi.interceptor;
|
||||
package org.ruoyi.common.mybatis.interceptor;
|
||||
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
||||
@@ -20,7 +20,7 @@ import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.session.ResultHandler;
|
||||
import org.apache.ibatis.session.RowBounds;
|
||||
import org.ruoyi.handler.PlusDataPermissionHandler;
|
||||
import org.ruoyi.common.mybatis.handler.PlusDataPermissionHandler;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@@ -35,16 +35,7 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class PlusDataPermissionInterceptor extends BaseMultiTableInnerInterceptor implements InnerInterceptor {
|
||||
|
||||
private final PlusDataPermissionHandler dataPermissionHandler;
|
||||
|
||||
/**
|
||||
* 构造函数,初始化 PlusDataPermissionHandler 实例
|
||||
*
|
||||
* @param mapperPackage 扫描的映射器包
|
||||
*/
|
||||
public PlusDataPermissionInterceptor(String mapperPackage) {
|
||||
this.dataPermissionHandler = new PlusDataPermissionHandler(mapperPackage);
|
||||
}
|
||||
private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler();
|
||||
|
||||
/**
|
||||
* 在执行查询之前,检查并处理数据权限相关逻辑
|
||||
@@ -64,7 +55,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
||||
return;
|
||||
}
|
||||
// 检查是否缺少有效的数据权限注解
|
||||
if (dataPermissionHandler.invalid(ms.getId())) {
|
||||
if (dataPermissionHandler.invalid()) {
|
||||
return;
|
||||
}
|
||||
// 解析 sql 分配对应方法
|
||||
@@ -92,7 +83,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
||||
return;
|
||||
}
|
||||
// 检查是否缺少有效的数据权限注解
|
||||
if (dataPermissionHandler.invalid(ms.getId())) {
|
||||
if (dataPermissionHandler.invalid()) {
|
||||
return;
|
||||
}
|
||||
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
|
||||
@@ -128,7 +119,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
||||
*/
|
||||
@Override
|
||||
protected void processUpdate(Update update, int index, String sql, Object obj) {
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), (String) obj, false);
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), false);
|
||||
if (null != sqlSegment) {
|
||||
update.setWhere(sqlSegment);
|
||||
}
|
||||
@@ -144,7 +135,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
||||
*/
|
||||
@Override
|
||||
protected void processDelete(Delete delete, int index, String sql, Object obj) {
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), (String) obj, false);
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), false);
|
||||
if (null != sqlSegment) {
|
||||
delete.setWhere(sqlSegment);
|
||||
}
|
||||
@@ -157,7 +148,7 @@ public class PlusDataPermissionInterceptor extends BaseMultiTableInnerIntercepto
|
||||
* @param mappedStatementId 映射语句的 ID
|
||||
*/
|
||||
protected void setWhere(PlainSelect plainSelect, String mappedStatementId) {
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), mappedStatementId, true);
|
||||
Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), true);
|
||||
if (null != sqlSegment) {
|
||||
plainSelect.setWhere(sqlSegment);
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package org.ruoyi.handler;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.MyBatisSystemException;
|
||||
import org.ruoyi.common.core.domain.R;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* Mybatis异常处理器
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class MybatisExceptionHandler {
|
||||
|
||||
/**
|
||||
* 主键或UNIQUE索引,数据重复异常
|
||||
*/
|
||||
@ExceptionHandler(DuplicateKeyException.class)
|
||||
public R<Void> handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage());
|
||||
return R.fail("数据库中已存在该记录,请联系管理员确认");
|
||||
}
|
||||
|
||||
/**
|
||||
* Mybatis系统异常 通用处理
|
||||
*/
|
||||
@ExceptionHandler(MyBatisSystemException.class)
|
||||
public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) {
|
||||
String requestURI = request.getRequestURI();
|
||||
String message = e.getMessage();
|
||||
if (StringUtils.contains("CannotFindDataSourceException", message)) {
|
||||
log.error("请求地址'{}', 未找到数据源", requestURI);
|
||||
return R.fail("未找到数据源,请联系管理员确认");
|
||||
}
|
||||
log.error("请求地址'{}', Mybatis系统异常", requestURI, e);
|
||||
return R.fail(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
package org.ruoyi.helper;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.ruoyi.common.core.exception.ServiceException;
|
||||
import org.ruoyi.common.core.utils.SpringUtils;
|
||||
import org.ruoyi.enums.DataBaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库助手
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class DataBaseHelper {
|
||||
|
||||
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
||||
|
||||
/**
|
||||
* 获取当前数据库类型
|
||||
*/
|
||||
public static DataBaseType getDataBaseType() {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String databaseProductName = metaData.getDatabaseProductName();
|
||||
return DataBaseType.find(databaseProductName);
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isMySql() {
|
||||
return DataBaseType.MY_SQL == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isOracle() {
|
||||
return DataBaseType.ORACLE == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isPostgerSql() {
|
||||
return DataBaseType.POSTGRE_SQL == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isSqlServer() {
|
||||
return DataBaseType.SQL_SERVER == getDataBaseType();
|
||||
}
|
||||
|
||||
public static String findInSet(Object var1, String var2) {
|
||||
DataBaseType dataBasyType = getDataBaseType();
|
||||
String var = Convert.toStr(var1);
|
||||
if (dataBasyType == DataBaseType.SQL_SERVER) {
|
||||
// charindex(',100,' , ',0,100,101,') <> 0
|
||||
return "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2);
|
||||
} else if (dataBasyType == DataBaseType.POSTGRE_SQL) {
|
||||
// (select strpos(',0,100,101,' , ',100,')) <> 0
|
||||
return "(select strpos(','||%s||',' , ',%s,')) <> 0".formatted(var2, var);
|
||||
} else if (dataBasyType == DataBaseType.ORACLE) {
|
||||
// instr(',0,100,101,' , ',100,') <> 0
|
||||
return "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var);
|
||||
}
|
||||
// find_in_set(100 , '0,100,101')
|
||||
return "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前加载的数据库名
|
||||
*/
|
||||
public static List<String> getDataSourceNameList() {
|
||||
return new ArrayList<>(DS.getDataSources().keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前连接的所有表名称
|
||||
*/
|
||||
public static List<String> getCurrentDataSourceTableNameList() {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
List<String> tableNames = new ArrayList<>();
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String catalog = conn.getCatalog();
|
||||
String schema = conn.getSchema();
|
||||
|
||||
// 获取所有表名
|
||||
try (var resultSet = metaData.getTables(catalog, schema, "%", new String[]{"TABLE"})) {
|
||||
while (resultSet.next()) {
|
||||
String tableName = resultSet.getString("TABLE_NAME");
|
||||
tableNames.add(tableName);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException("获取表名称失败: " + e.getMessage());
|
||||
}
|
||||
return tableNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定表的字段信息
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 字段信息列表
|
||||
*/
|
||||
public static List<Map<String, Object>> getTableColumnInfo(String tableName) {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
List<Map<String, Object>> columns = new ArrayList<>();
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String catalog = conn.getCatalog();
|
||||
String schema = conn.getSchema();
|
||||
|
||||
// 获取表字段信息
|
||||
try (ResultSet resultSet = metaData.getColumns(catalog, schema, tableName, "%")) {
|
||||
while (resultSet.next()) {
|
||||
Map<String, Object> column = new HashMap<>();
|
||||
column.put("columnName", resultSet.getString("COLUMN_NAME"));
|
||||
column.put("columnComment", resultSet.getString("REMARKS"));
|
||||
column.put("dataType", resultSet.getString("TYPE_NAME"));
|
||||
column.put("columnSize", resultSet.getInt("COLUMN_SIZE"));
|
||||
column.put("isNullable", "YES".equals(resultSet.getString("IS_NULLABLE")));
|
||||
column.put("ordinalPosition", resultSet.getInt("ORDINAL_POSITION"));
|
||||
|
||||
// 设置默认值
|
||||
String defaultValue = resultSet.getString("COLUMN_DEF");
|
||||
column.put("columnDefault", defaultValue);
|
||||
|
||||
columns.add(column);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取主键信息
|
||||
try (ResultSet pkResultSet = metaData.getPrimaryKeys(catalog, schema, tableName)) {
|
||||
List<String> primaryKeys = new ArrayList<>();
|
||||
while (pkResultSet.next()) {
|
||||
primaryKeys.add(pkResultSet.getString("COLUMN_NAME"));
|
||||
}
|
||||
|
||||
// 标记主键字段
|
||||
for (Map<String, Object> column : columns) {
|
||||
String columnName = (String) column.get("columnName");
|
||||
column.put("isPrimaryKey", primaryKeys.contains(columnName));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException("获取表字段信息失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
org.ruoyi.config.MybatisPlusConfig
|
||||
org.ruoyi.common.mybatis.config.MybatisPlusConfig
|
||||
|
||||
Reference in New Issue
Block a user