Mybatis-plus逻辑删除更新字段

MybatisPlus版本

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.2</version>
</dependency>

创建LogicBatchDeleteWithFill

public class LogicBatchDeleteWithFill extends AbstractMethod {
    private static final String MAPPER_METHOD = "batchDeleteWithFill";

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
        if (tableInfo.isWithLogicDelete()) {
            List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
                    .filter(i -> i.getFieldFill() == FieldFill.UPDATE || i.getFieldFill() == FieldFill.INSERT_UPDATE)
                    .collect(toList());
            if (CollectionUtils.isNotEmpty(fieldInfos)) {
                String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY))
                        + tableInfo.getLogicDeleteSql(false, false);
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet," ",
                        sqlWhereEntityWrapper(true, tableInfo));
            } else {
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), "",
                        sqlWhereEntityWrapper(true, tableInfo));
            }
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
            return this.addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
        } else {
            sqlMethod = SqlMethod.DELETE;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName()," ", sqlWhereEntityWrapper(true, tableInfo));
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
            return this.addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
        }
    }
}

创建LogicDeleteSqlInjector

public class LogicDeleteSqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new LogicDeleteByIdWithFill());
        methodList.add(new LogicBatchDeleteWithFill());
        methodList.add(new AlwaysUpdateSomeColumnById());
        return methodList;
    }
}

 创建MyBaseMapper

public interface MyBaseMapper<T> extends BaseMapper<T> {
    /**
     * 根据id逻辑删除并填充字段
     * @param entity
     * @return
     */
    int deleteByIdWithFill(T entity );
 
    /**
     * 批量逻辑删除并填充字段
     * @param entity
     * @param wrapper
     * @return
     */
    int batchDeleteWithFill(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> wrapper);
}

配置

public class MybatisPlusConfig {

   /**  省略代码 **/

    /**
     * 逻辑删除
     * @return
     */
    @Bean
    public LogicDeleteSqlInjector logicDeleteSqlInjector() {
        return new LogicDeleteSqlInjector();
    }
}

继承MyBaseMapper

public interface SysOperLogMapper extends MyBaseMapper<SysOperLog> {
}

至此扩展完成,在调用mapper中时,可以同时使用扩展方法及mybatis-plus自带的方法。

需要扩展service的下面链接自取:

mybatis-plus扩展IServiceicon-default.png?t=N176https://blog.csdn.net/qq_43040552/article/details/129276408

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值