Skip to content

CreatedEvent 注解 VO创建后的事件

说明

CreatedEvent 是VO创建后的事件,可以对VO进行操作,例如设置枚举,其他等等 都可以

属性说明

属性是否可空默认值说明
value指定类进行监听,需要实现一个onCreatedEvent静态方法,参数为所在直接的VO类

如何用?

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

    private Integer id;

    private String userName;

    private String password;

    private Integer status;

    @Ignore
    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 CreatedEventFactory{
    
    //方法1 
    public static void onCreatedEvent(SysUserVo sysUserVo) {
        sysUserVo.setStatusName("我的状态名称");
    }
    //方法2 缓存中间数据,加快onCreatedEvent方法
    public static void onCreatedEvent(Map<String, Object> context,SysUserVo sysUserVo) {
        Object value=context.computeIfAbsent(sysUserVo.getId(),key->redis.get(sysUserVo.getId));
        sysUserVo.setxxx(value.toString());
    }
}
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));
    }
}