java 实体属性复制方法

//1、使用更新对象的非空值去覆盖待更新对象
StringUtils.copyPropertiesIgnoreNull(device, dev); //用device对象去覆盖dev对象


复制属性:将attr实体中的属性一一拷贝给attrEntity 实体,名字要一致。
eg:
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr, attrEntity);
this.save(attrEntity);
}


eg:
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
// relationDao.delete(new QueryWrapper<>().eq("attr_id", 1L).eq("attr_group_id", 1L));
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
relationDao.deleteBatchRelation(entities);
}


//源码
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}


//2、使用更新对象的非空值去覆盖待更新对象
实体拷贝:
UserDTO o = BeanUtil.copyProperties(user, UserDTO.class);

@Data
public class UserDTO {
private Long id;
private String nickName;
private String icon;
}


@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_user")
public class User implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;

/**
* 手机号码
*/
private String phone;

/**
* 密码,加密存储
*/
private String password;

/**
* 昵称,默认是随机字符
*/
private String nickName;

/**
* 用户头像
*/
private String icon = "";

/**
* 创建时间
*/
private LocalDateTime createTime;

/**
* 更新时间
*/
private LocalDateTime updateTime;


}
 




posted @   sensen~||^_^|||&  阅读(981)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示