|
@@ -5,7 +5,6 @@ import java.sql.Connection;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
import java.util.Properties;
|
|
|
|
|
|
-import com.huashe.common.annotation.mybatis.Tenant;
|
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
@@ -18,10 +17,11 @@ import org.apache.ibatis.reflection.MetaObject;
|
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import com.huashe.common.annotation.mybatis.Tenant;
|
|
import com.huashe.park.common.animations.mybatis.MethodMetadataCache;
|
|
import com.huashe.park.common.animations.mybatis.MethodMetadataCache;
|
|
|
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
-import net.sf.jsqlparser.expression.JdbcParameter;
|
|
|
|
|
|
+import net.sf.jsqlparser.expression.LongValue;
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
@@ -49,8 +49,7 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
|
|
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
|
|
Method mapperMethod = getMapperMethod(mappedStatement);
|
|
Method mapperMethod = getMapperMethod(mappedStatement);
|
|
// 判断是否需要添加租户条件
|
|
// 判断是否需要添加租户条件
|
|
- if (ObjectUtils.isNotEmpty(mapperMethod)) {
|
|
|
|
-
|
|
|
|
|
|
+ if (ObjectUtils.allNotNull(mapperMethod, getLoginUser().getUser().getTenantId())) {
|
|
// 解析 SQL 语法树
|
|
// 解析 SQL 语法树
|
|
Statement stmt = CCJSqlParserUtil.parse(originalSql);
|
|
Statement stmt = CCJSqlParserUtil.parse(originalSql);
|
|
if (stmt instanceof Select) {
|
|
if (stmt instanceof Select) {
|
|
@@ -61,7 +60,7 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
// 构建租户条件表达式(tenant_id = ?)
|
|
// 构建租户条件表达式(tenant_id = ?)
|
|
EqualsTo tenantCondition = new EqualsTo();
|
|
EqualsTo tenantCondition = new EqualsTo();
|
|
tenantCondition.setLeftExpression(handleColumn(mapperMethod));
|
|
tenantCondition.setLeftExpression(handleColumn(mapperMethod));
|
|
- tenantCondition.setRightExpression(new JdbcParameter());
|
|
|
|
|
|
+ tenantCondition.setRightExpression(new LongValue(getLoginUser().getUser().getTenantId()));
|
|
|
|
|
|
// 修改 WHERE 条件
|
|
// 修改 WHERE 条件
|
|
Expression where = plainSelect.getWhere();
|
|
Expression where = plainSelect.getWhere();
|
|
@@ -78,11 +77,6 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
// 生成修改后的 SQL
|
|
// 生成修改后的 SQL
|
|
String modifiedSql = plainSelect.toString();
|
|
String modifiedSql = plainSelect.toString();
|
|
metaObject.setValue("delegate.boundSql.sql", modifiedSql);
|
|
metaObject.setValue("delegate.boundSql.sql", modifiedSql);
|
|
-
|
|
|
|
- // 添加租户参数(动态绑定到 PreparedStatement)
|
|
|
|
- List<Object> parameters = (List<Object>) metaObject.getValue("delegate.boundSql.parameterObject");
|
|
|
|
-
|
|
|
|
- parameters.add(getLoginUser().getUser().getTenantId()); // 租户值
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -124,4 +118,8 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
@Override
|
|
@Override
|
|
public void setProperties(Properties properties) {
|
|
public void setProperties(Properties properties) {
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|