插件注入

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.session.Configuration;

import java.util.List;

public class MybatisPlusSqlInjector extends DefaultSqlInjector {
    
    @Override
    public List<AbstractMethod> getMethodList(Configuration configuration, Class<?> mapperClass, TableInfo tableInfo) {
        final List<AbstractMethod> methods = super.getMethodList(configuration, mapperClass, tableInfo);
        methods.add(new WrightLogicDeleteByIdWithFill());
        return methods;
    }
}

删除填充方法

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.List;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class WrightLogicDeleteByIdWithFill extends AbstractMethod {
    
    public WrightLogicDeleteByIdWithFill() {
        super("deleteByIdWithFill");
    }
    
    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public WrightLogicDeleteByIdWithFill(String name) {
        super(name);
    }
    
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
        if (tableInfo.isWithLogicDelete()) {
            List<TableFieldInfo> fieldInfos = tableInfo.getFieldList()
                    .stream()
                    .filter(TableFieldInfo::isWithUpdateFill)
                    .filter(f -> !f.isLogicDelete())
                    .collect(toList());
            if (CollectionUtils.isNotEmpty(fieldInfos)) {
                String sqlSet = "SET " + fieldInfos.stream()
                        .map(i -> i.getSqlSet(EMPTY))
                        .collect(joining(EMPTY)) + tableInfo.getLogicDeleteSql(false, false);
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(),
                        tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
            } else {
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
                        tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
            }
        } else {
            sqlMethod = SqlMethod.DELETE_BY_ID;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
                    tableInfo.getKeyProperty());
        }
        SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass);
        return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
    }
}

自定义Mapper

public interface MyBaseMapper<Entity>  extends BaseMapper<Entity> {
    /**
     * 逻辑删除填充其他字段的值
     *
     * @param entity 要删除的实体对象
     * @return 受影响记录数量
     */
    int deleteByIdWithFill(Entity entity);
}

自定义Service

 public interface MyBaseService<Entity> extends IService<Entity> {
    /**
     * 逻辑删除填充其他字段的值
     *
     * @param entity 要删除的实体对象
     * @return 受影响记录数量
     */
    boolean deleteByIdWithFill(Entity entity);
}

自定义 Service Mapper

    /**
     * 根据ID逻辑删除
     *
     * @param id 主键ID
     * @return 是否删除成功
     */
    default boolean deleteLogicById(Integer id) {
        // 使用 UpdateWrapper 构建更新条件
        UpdateWrapper<Entity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("is_deleted", 1)
                .set("deleted_by", StpUtil.getLoginIdAsInt())
                .set("deleted_at", LocalDateTime.now());
        
        // 直接更新 此方法无法进行自动填充
        return this.update(updateWrapper) > 0;
    }

插件方法

mybatis plus 增删改自动填充字段值 - myEsn2E9 - 博客园