Skip to content

PutValue 注解 VO注入动态值

说明

PutValue 是VO注解,不能应用在实体类上,用于根据N列注入一个动态值的场景;此注解会根据值,method,factory做session级的缓存,请注意!

属性说明

属性是否可空默认值说明
source来源,对应的实体类
property对应的实体类的字段名
storey1存储层级,联表自己join自己时使用,一般不用管
factory目标类 需要实现 指定getValueMethod名字的静态方法
method指定在factory里获取值的方法
requiredfalse是否必须,true时,找不到对应的枚举会报错
defaultValue找不到时,默认值

如何用?

java
@Data
@ResultEntity(SysUser.class)
public class SysUserVo {

    private Integer id;
    
    private String userName;

    private String password;
    
    private Integer status;

    @PutValue(source = SysUser.class, property = "id", factory = GetPutValueFactory.class, method = "getStatusName")
    private String statusName;
}
java
public enum StatusEnum {
    
    ENABLE(1,"正常"),UNABLE(0,"禁用");
    
    private final Integer code;
    
    private final String name;

    StatusEnum(Integer code,String name){
        this.code= code;
        this.name=name;
    }
    
}
java
public class GetPutValueFactory {
    public static String getStatusName(Integer code) {
        Optional<StatusEnum> e = Arrays.stream(StatusEnum.values())
                .filter(item -> item.getCode().equals(code))
                .findFirst();
        return e.map(PutValueEnum::getName).orElse(null);
    }
}
java
@Service
public class Test{

    @Autowired
    private SysUserMapper sysUserMapper;

    public void demo() {
        Pager<SysUserVo> pager = QueryChain.of(sysUserMapper)
                .eq(SysUser::getId,1)
                .like(SysUser::getUserName,"xxx")
                .returnType(SysUserVo.class)
                .paging(Pager.of(1,10));
    }
}

框架自动帮你 注入StatusEnum枚举对应的name名字