目的:为当前用户产生的所有删除操作附加用户id作为删除添加。
环境:springboot:2.4.10,mybatis-plus:3.5.1
参考 TenantLineInnerInterceptor 类,精简其中的操作。
拦截类:DeleteInnerInterceptor
STORE_ID:需要附加的处理字段
CHECK_TABLES:需要进行处理的表
/**
* 删除操作拦截器
*/
@Slf4j
public class DeleteInnerInterceptor implements InnerInterceptor {
private static final String STORE_ID = "store_id";
private List<String> CHECK_TABLES;
public DeleteInnerInterceptor(List<String> CHECK_TABLES) {
this.CHECK_TABLES = CHECK_TABLES;
}
@Override
public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh);
MappedStatement ms = mpSh.mappedStatement();
SqlCommandType sct = ms.getSqlCommandType();
if (sct == SqlCommandType.DELETE) {
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
mpBs.sql(this.appendWhere(mpBs.sql()));
}
}
protected String appendWhere(String sql) {
//当前用户
AuthUser authUser = UserContext.getCurrentUser();
//如果是店铺
if (authUser.getRole().equals(UserEnums.STORE)) {
//店铺需要进行删除某些表操作时,增加store_id = 条件
try {
StringBuilder sb = new StringBuilder();
Statements statements = CCJSqlParserUtil.parseStatements(sql);
int i = 0;
for (Iterator var6 = statements.getStatements().iterator(); var6.hasNext(); ++i) {
Statement statement = (Statement) var6.next();
if (i > 0) {
sb.append(";");
}
sb.append(this.processParser(statement, authUser.getStoreId(), STORE_ID));
}
log.info("parse the finished SQL: " + sb.toString());
return sb.toString();
} catch (JSQLParserException var8) {
throw ExceptionUtils.mpe("Failed to process, Error SQL: %s", var8.getCause(), new Object[]{sql});
}
}
return sql;
}
protected String processParser(Statement statement, String val, String columnVal) {
if (statement instanceof Delete) {
this.processDelete((Delete) statement, val, columnVal);
}
String sql = statement.toString();
return sql;
}
protected void processDelete(Delete delete, String val, String columnVal) {
if (this.containTable(delete.getTable().getName())) {
delete.setWhere(this.andExpression(delete.getTable(), delete.getWhere(), val, columnVal));
}
}
protected boolean containTable(String tableName) {
return CHECK_TABLES.contains(tableName);
}
protected BinaryExpression andExpression(Table table, Expression where, String val, String columnVal) {
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(this.getAliasColumn(table, columnVal));
equalsTo.setRightExpression(new StringValue(val));
if (null != where) {
return where instanceof OrExpression ? new AndExpression(equalsTo, new Parenthesis(where)) : new AndExpression(equalsTo, where);
} else {
return equalsTo;
}
}
protected Column getAliasColumn(Table table, String columnVal) {
StringBuilder column = new StringBuilder();
if (table.getAlias() != null) {
column.append(table.getAlias().getName());
} else {
column.append(table.getName());
}
column.append(".").append(columnVal);
return new Column(column.toString());
}
}
配置类:MybatisPlusConfig
@Configuration
@MapperScan({"cn.base.*.*.mapper"})
public class MybatisPlusConfig {
@Autowired
DeleteProperties deleteProperties;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new DeleteInnerInterceptor(deleteProperties.getTableNames()));
return interceptor;
}
}
@Data
@Configuration
@ConfigurationProperties(prefix = "delete-inner-interceptor")
public class DeleteProperties {
private List<String> tableNames = new ArrayList<>();
}
当前需求只拦截Delete操作,可改进添加其他操作处理;