mybatisplus删除操作拦截器

目的:为当前用户产生的所有删除操作附加用户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操作,可改进添加其他操作处理;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值