✨ Introducing new features. #2005 升级 mybatis-plus 3.4.1 支持插件主体配置设置 e5dd87ab66
This commit is contained in:
1 parent
a6c26b7bca
commit
c03076e711
3 files changed
+89
-155
No files matched your search
+79
@@ -0,0 +1,79 @@
|
||||
package com.pig4cloud.pigx.common.data.datascope;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import lombok.Setter;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.session.ResultHandler;
|
||||
import org.apache.ibatis.session.RowBounds;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/11/29
|
||||
*/
|
||||
public class DataScopeInnerInterceptor implements InnerInterceptor {
|
||||
|
||||
@Setter
|
||||
private DataScopeHandle dataScopeHandle;
|
||||
|
||||
@Override
|
||||
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
|
||||
ResultHandler resultHandler, BoundSql boundSql) {
|
||||
PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
|
||||
|
||||
String originalSql = boundSql.getSql();
|
||||
Object parameterObject = boundSql.getParameterObject();
|
||||
|
||||
// 查找参数中包含DataScope类型的参数
|
||||
DataScope dataScope = findDataScopeObject(parameterObject);
|
||||
if (dataScope == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String scopeName = dataScope.getScopeName();
|
||||
List<Integer> deptIds = dataScope.getDeptIds();
|
||||
// 优先获取赋值数据
|
||||
if (CollUtil.isEmpty(deptIds) && dataScopeHandle.calcScope(deptIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (deptIds.isEmpty()) {
|
||||
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2",
|
||||
dataScope.getFunc().getType(), originalSql);
|
||||
}
|
||||
else {
|
||||
String join = CollectionUtil.join(deptIds, ",");
|
||||
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)",
|
||||
dataScope.getFunc().getType(), originalSql, scopeName, join);
|
||||
}
|
||||
|
||||
mpBs.sql(originalSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找参数是否包括DataScope对象
|
||||
* @param parameterObj 参数列表
|
||||
* @return DataScope
|
||||
*/
|
||||
private DataScope findDataScopeObject(Object parameterObj) {
|
||||
if (parameterObj instanceof DataScope) {
|
||||
return (DataScope) parameterObj;
|
||||
}
|
||||
else if (parameterObj instanceof Map) {
|
||||
for (Object val : ((Map<?, ?>) parameterObj).values()) {
|
||||
if (val instanceof DataScope) {
|
||||
return (DataScope) val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
-138
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.data.datascope;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
||||
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/5/27
|
||||
* <p>
|
||||
* mybatis 数据权限拦截器
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Intercepts({
|
||||
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
|
||||
public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor {
|
||||
|
||||
private final DataScopeHandle dataScopeHandle;
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Object intercept(Invocation invocation) {
|
||||
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
|
||||
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
|
||||
this.sqlParser(metaObject);
|
||||
// 先判断是不是SELECT操作
|
||||
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
|
||||
if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
|
||||
String originalSql = boundSql.getSql();
|
||||
Object parameterObject = boundSql.getParameterObject();
|
||||
|
||||
// 查找参数中包含DataScope类型的参数
|
||||
DataScope dataScope = findDataScopeObject(parameterObject);
|
||||
if (dataScope == null) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
String scopeName = dataScope.getScopeName();
|
||||
List<Integer> deptIds = dataScope.getDeptIds();
|
||||
// 优先获取赋值数据
|
||||
if (CollUtil.isEmpty(deptIds) && dataScopeHandle.calcScope(deptIds)) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
if (deptIds.isEmpty()) {
|
||||
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2",
|
||||
dataScope.getFunc().getType(), originalSql);
|
||||
}
|
||||
else {
|
||||
String join = CollectionUtil.join(deptIds, ",");
|
||||
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)",
|
||||
dataScope.getFunc().getType(), originalSql, scopeName, join);
|
||||
}
|
||||
|
||||
metaObject.setValue("delegate.boundSql.sql", originalSql);
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成拦截对象的代理
|
||||
* @param target 目标对象
|
||||
* @return 代理对象
|
||||
*/
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
if (target instanceof StatementHandler) {
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis配置的属性
|
||||
* @param properties mybatis配置的属性
|
||||
*/
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找参数是否包括DataScope对象
|
||||
* @param parameterObj 参数列表
|
||||
* @return DataScope
|
||||
*/
|
||||
private DataScope findDataScopeObject(Object parameterObj) {
|
||||
if (parameterObj instanceof DataScope) {
|
||||
return (DataScope) parameterObj;
|
||||
}
|
||||
else if (parameterObj instanceof Map) {
|
||||
for (Object val : ((Map<?, ?>) parameterObj).values()) {
|
||||
if (val instanceof DataScope) {
|
||||
return (DataScope) val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+10
-17
@@ -22,7 +22,7 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeHandle;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeInterceptor;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeInnerInterceptor;
|
||||
import com.pig4cloud.pigx.common.data.datascope.DataScopeSqlInjector;
|
||||
import com.pig4cloud.pigx.common.data.datascope.PigxDefaultDatascopeHandle;
|
||||
import com.pig4cloud.pigx.common.data.resolver.SqlFilterArgumentResolver;
|
||||
@@ -58,19 +58,18 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据权限插件 优先 mybatis-plus 的拦截器执行
|
||||
* @return DataScopeInterceptor
|
||||
* pigx 默认数据权限处理器
|
||||
* @return PigxDefaultDatascopeHandle
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBean(DataScopeHandle.class)
|
||||
public DataScopeInterceptor dataScopeInterceptor() {
|
||||
return new DataScopeInterceptor(dataScopeHandle());
|
||||
public DataScopeHandle dataScopeHandle() {
|
||||
return new PigxDefaultDatascopeHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis plus 拦截器配置
|
||||
* @return
|
||||
* @return PigxDefaultDatascopeHandle
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
@@ -79,6 +78,10 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
|
||||
TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
|
||||
tenantLineInnerInterceptor.setTenantLineHandler(pigxTenantHandler());
|
||||
interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
|
||||
// 数据权限
|
||||
DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
|
||||
dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle());
|
||||
interceptor.addInnerInterceptor(dataScopeInnerInterceptor);
|
||||
// 分页支持
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
@@ -94,16 +97,6 @@ public class MybatisPlusConfiguration implements WebMvcConfigurer {
|
||||
return new PigxTenantHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* pigx 默认数据权限处理器
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DataScopeHandle dataScopeHandle() {
|
||||
return new PigxDefaultDatascopeHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展 mybatis-plus baseMapper 支持数据权限
|
||||
* @return
|
||||
|
||||
Reference in new issue
Block a user