bean-searcher整合mybatis-plus逻辑删除

2022-08-21 13:59:50 浏览数 (1)

需要用到参数过滤器:

https://bs.zhxu.cn/guide/latest/advance.html#参数过滤器

代码语言:javascript复制
@Bean
public ParamFilter logicDeleteFilter() {
    return new ParamFilter() {
        @Override
        public <T> Map<String, Object> doFilter(BeanMeta<T> beanMeta, Map<String, Object> paraMap) {
            // beanMeta 是正在检索的实体类的元信息, paraMap 是当前的检索参数
            // 返回过滤后的检索参数
            TableInfo tableInfo = TableInfoHelper.getTableInfo(beanMeta.getBeanClass());
            Opt.ofNullable(tableInfo).ifPresent(info -> {
                TableFieldInfo logicDelField = tableInfo.getLogicDeleteFieldInfo();
                String logicNoDelVal = logicDelField.getLogicNotDeleteValue();
                if (StringPool.NULL.equalsIgnoreCase(logicNoDelVal)) {
                    MapUtils.builder(paraMap).field(logicDelField.getProperty()).op(IsNull.class);
                } else {
                    MapUtils.builder(paraMap).field(logicDelField.getProperty(), logicNoDelVal);
                }
            });
            return paraMap;
        }
    };
}

0 人点赞