mybatis-plus简化状态管理代码(附代码分析)

59 阅读1分钟

image.png

package com.example.demo.exper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.function.Consumer;

/**
 * 通用状态转移工具类
 * @param <T> 实体类型
 * @param <E> 状态枚举类型
 */
public class StateTransitionHelper<T extends StatefulEntity, E extends EnableStateful> {

    private final IService<T> service;

    public StateTransitionHelper(IService<T> service) {
        this.service = service;
    }

    /**
     * 执行状态转移
     * @param id 实体ID
     * @param expectedCurrentStatus 期望当前状态(可为null)
     * @param targetStatus 目标状态
     * @param wrapperConsumer 额外的更新条件
     * @return 是否更新成功
     */
    public boolean transitionStatus(Long id, E expectedCurrentStatus, E targetStatus, 
                                   Consumer<LambdaUpdateWrapper<T>> wrapperConsumer) {
        LambdaUpdateWrapper<T> wrapper = buildBaseWrapper(id, expectedCurrentStatus, targetStatus);
        if (wrapperConsumer != null) {
            wrapperConsumer.accept(wrapper);
        }
        return service.update(wrapper);
    }

    /**
     * 构建基础更新条件
     */
    private LambdaUpdateWrapper<T> buildBaseWrapper(Long id, E expectedCurrentStatus, E targetStatus) {
        LambdaUpdateWrapper<T> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StatefulEntity::getId, id);

        if (expectedCurrentStatus != null) {
            wrapper.eq(StatefulEntity::getStatus, expectedCurrentStatus);
        }
        
        return wrapper.set(StatefulEntity::getStatus, targetStatus);
    }

    /**
     * 直接通过实体对象执行状态转移(带乐观锁检查)
     * @param entity 实体对象
     * @param targetStatus 目标状态
     * @return 是否更新成功
     */
    public boolean transitionStatus(T entity, E targetStatus) {
        entity.setStatus(targetStatus.getStatus());
        return service.updateById(entity);
    }
}
package com.example.demo.exper;

public interface StatefulEntity{
    Long getId();
    Integer getStatus();

    void setStatus(Integer status);

}
package com.example.demo.exper;

/**
 * @author : zlx
 * description :
 * date : 2025-05-08 13:26
 **/
public interface EnableStateful {
    Integer getStatus() ;
}
@AllArgsConstructor
@Getter
public enum FileWorkFlowEnum implements EnableStateful 

附带一个文件模板

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author : zlx
* description : ${description}
* date : ${YEAR}-${MONTH}-${DAY}
**/

@AllArgsConstructor
@Getter
public enum ${NAME}Enum {
    ; 
     
    private final Integer status;
    private final String desc;

    private static Map<Integer, ${NAME}Enum> cache;

    static {
        cache = Arrays.stream(${NAME}Enum.values()).collect(Collectors.toMap(${NAME}Enum::getStatus, Function.identity()));
    }

    public static ${NAME}Enum of(Integer type) {
        return cache.get(type);
    }
}

代码分析

为什么不会发生循环依赖

循环依赖是两个bean相互注入,初始1,初始2,初始1 形成循环依赖

这个bean的初始化就是注入的IService,这个bean就已经加载完成了,这个bean不会在容器中去寻找并加载这个IService了

StateTransitionHelper