mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-26 11:51:55 +08:00
共通字段自动填充
This commit is contained in:
@@ -1,12 +1,5 @@
|
||||
package com.ruoyi.common.datascope.aspect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.ruoyi.common.core.context.SecurityContextHolder;
|
||||
import com.ruoyi.common.core.text.Convert;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
@@ -16,6 +9,13 @@ import com.ruoyi.common.security.utils.SecurityUtils;
|
||||
import com.ruoyi.system.api.domain.SysRole;
|
||||
import com.ruoyi.system.api.domain.SysUser;
|
||||
import com.ruoyi.system.api.model.LoginUser;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 数据过滤处理
|
||||
@@ -86,7 +86,7 @@ public class DataScopeAspect {
|
||||
*/
|
||||
public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias, String permission) {
|
||||
StringBuilder sqlString = new StringBuilder();
|
||||
List<String> conditions = new ArrayList<String>();
|
||||
List<String> conditions = new ArrayList<>();
|
||||
|
||||
for (SysRole role : user.getRoles()) {
|
||||
String dataScope = role.getDataScope();
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.ruoyi.common.datascope.mybatis;
|
||||
|
||||
import com.ruoyi.common.core.utils.DateUtils;
|
||||
import com.ruoyi.common.core.web.domain.BaseEntity;
|
||||
import com.ruoyi.common.core.web.domain.ExtBaseEntity;
|
||||
import com.ruoyi.common.security.utils.SecurityUtils;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.plugin.Interceptor;
|
||||
import org.apache.ibatis.plugin.Intercepts;
|
||||
import org.apache.ibatis.plugin.Invocation;
|
||||
import org.apache.ibatis.plugin.Signature;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 共通字段自动填充插件
|
||||
* <p>
|
||||
* 注:该插件仅针对自己写的mapper.xml里的insert和update语句生效,对Dynamic SQL无效
|
||||
* </p>
|
||||
*
|
||||
* @author Alan Scipio
|
||||
* created on 2024/2/6
|
||||
*/
|
||||
@Intercepts({
|
||||
@Signature(
|
||||
type = Executor.class,
|
||||
method = "update",
|
||||
args = {MappedStatement.class, Object.class}
|
||||
)
|
||||
})
|
||||
public class AutoFillPlugin implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
Object parameter = invocation.getArgs()[1];
|
||||
if (parameter instanceof BaseEntity entity) {
|
||||
MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
|
||||
String commandType = statement.getSqlCommandType().name();
|
||||
|
||||
Date now = DateUtils.getNowDate();
|
||||
String userId = SecurityUtils.getUserIdStr();
|
||||
|
||||
if ("INSERT".equals(commandType)) {
|
||||
// insert时的自动填充
|
||||
entity.setCommonForInsert(userId, now);
|
||||
} else if ("UPDATE".equals(commandType)) {
|
||||
// update时的自动填充
|
||||
entity.setCommonForUpdate(userId, now);
|
||||
}
|
||||
|
||||
if (entity instanceof ExtBaseEntity extEntity) {
|
||||
// ExtBaseEntity的自动填充
|
||||
if (extEntity.getUpdateCount() == null) {
|
||||
extEntity.setUpdateCount(0);
|
||||
} else {
|
||||
extEntity.setUpdateCount(extEntity.getUpdateCount() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ruoyi.common.datascope.mybatis;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Alan Scipio
|
||||
* created on 2024/2/6
|
||||
*/
|
||||
@Configuration
|
||||
public class MyBatisConfig {
|
||||
|
||||
@Resource
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
//添加MyBatis插件
|
||||
AutoFillPlugin autoFillPlugin = new AutoFillPlugin();
|
||||
sqlSessionFactory.getConfiguration().addInterceptor(autoFillPlugin);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
com.ruoyi.common.datascope.aspect.DataScopeAspect
|
||||
com.ruoyi.common.datascope.mybatis.MyBatisConfig
|
||||
|
||||
Reference in New Issue
Block a user