1.配置mybatisplus自动装配,添加sql拦截器
在这里插入图片描述

public class JinchaoyunMybatisAutoConfiguration {
// 逻辑删除插件配置
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 添加自定义的拦截器,用于处理逻辑删除字段
        mybatisPlusInterceptor.addInnerInterceptor(new InnerInterceptor() {
            @Override
            public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
                                    ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
                String sql = boundSql.getSql();
                try {
                    // 如果 SQL 中包含 "no deleted" 标记
                    if(sql.contains(IS_DELETED)||sql.contains(IS_DELETEDS)){
                        // 使用 JSqlParser 解析 SQL
                        Statement statement = CCJSqlParserUtil.parse(sql);

                        if (statement instanceof Select) {
                            PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody();

                            // 如果 WHERE 子句存在,则替换逻辑删除条件
                            if (plainSelect.getWhere() != null) {
                                String modifiedWhere = plainSelect.getWhere().toString()
                                        .replace("deleted = 0", "deleted = 1")  // 替换 "deleted = 0" 为 "deleted = 1"
                                        .replace(IS_DELETED, "")  // 替换 "and 1 = 2" 为空字符串 标识不能加到sql中,处理and 1 = 2大小写不一致的情况
                                        .replace(IS_DELETEDS, "");  // // 替换 "and 1 = 2" 为空字符串 标识不能加到sql中,处理and 1 = 2大小写不一致的情况
                                plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(modifiedWhere));
                            }
                        }

                        // 更新 SQL
                        MetaObject metaObject = SystemMetaObject.forObject(boundSql);
                        metaObject.setValue("sql", statement.toString());
                    }
                } catch (Exception e) {
                    throw new RuntimeException("SQL 解析失败", e);
                }
            }
        });

        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件
        // 添加自定义的拦截器
        return mybatisPlusInterceptor;
    }
}    

2.配置全局静态常量,用于查询逻辑已删除数据
在这里插入图片描述

public static final String IS_DELETED= "and 1 = 2";// 只查询已逻辑删除的数据,小写
public static final String IS_DELETEDS= "AND 1 = 2";// 只查询已逻辑删除的数据,大写

3.使用当前配置的自动查询逻辑删除
在这里插入图片描述

.last(null!=reqVO.getDeleted()&&reqVO.getDeleted()==true,IS_DELETED)// 只查询已被删除的设备
.orderByDesc(null==reqVO.getDeleted(),CdnDeviceDO::getId));// 如果没有传入是否查询删除数据和不查询逻辑删除的设备,则按照id降序排列

4.然后遇到查询已删除数据的详情,是查询不到的,所以还要继续拦截sql语句,进行查询替换deleted=1
常量类中继续添加
在这里插入图片描述

public static String GET_DELETED= “”;// 自动拼接deleted=1,表示查询已经删除的设备数据,用于查询数据详情使用

在这里插入图片描述
代码逻辑处理:
在这里插入图片描述

警告:last方法和orderByDesc两者有任选其一执行,否则查询逻辑删除数据会有问题,所以我判断为逻辑删除入参deleted空的时候才会去执行orderByDesc

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐