BeanCopyUtil
package com.rscode.credits.util; import java.util.HashSet;
import java.util.Set; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
/**
* @author 作者 tn
* @version 创建时间:2018年11月13日
* 类说明 复制bean
*/
public class BeanCopyUtil {
//source中的非空属性复制到target中
/**
*
* @param source 源
* @param target 目标
*/
public static <T> void beanCopy(T source, T target) {
BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
}
//source中的非空属性复制到target中,但是忽略指定的属性,也就是说有些属性是不可修改的(个人业务需要)
public static <T> void beanCopyWithIngore(T source, T target, String... ignoreProperties) {
String[] pns = getNullAndIgnorePropertyNames(source, ignoreProperties);
BeanUtils.copyProperties(source, target, pns);
}
public static String[] getNullAndIgnorePropertyNames(Object source, String... ignoreProperties) {
Set<String> emptyNames = getNullPropertyNameSet(source);
for (String s : ignoreProperties) {
emptyNames.add(s);
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static String[] getNullPropertyNames(Object source) {
Set<String> emptyNames = getNullPropertyNameSet(source);
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static Set<String> getNullPropertyNameSet(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
return emptyNames;
}
}
BeanCopyUtil的更多相关文章
- java 数据脱敏
所谓数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护.在涉及客户安全数据或者一些商业性敏感数据的情况下,在不违反系统规则条件下,对真实数据进行改造并提供测试使用,如身份 ...
- BeanUtils 如何拷贝 List?
BeanUtils 如何拷贝 List? 一.背景 我们在DO.Model.VO层数据间可能经常转换数据: Entity对应的是持久层数据结构(一般是数据库表的映射模型); Model 对应的是业务层 ...
- 【Springboot】FastJson与Jackson全局序列化方式的配置和相关工具类
springboot 版本: <parent> <groupId>org.springframework.boot</groupId> <artifactId ...
随机推荐
- idea springboot jrebel hotreloaded
http://127.0.0.1:8888/88414687-3b91-4286-89ba-2dc813b107ce
- 简述 JVM 垃圾回收算法
经典垃圾回收 标记-清除(Mark-Sweep) 研发园开了家新餐厅,餐厅老板在考虑如何回收餐盘时首先使用了最简单的方式,那就是服务员在顾客用餐的过程中,不定时的观察餐厅,针对用完餐的顾客记录他们的位 ...
- 安装vue 教程
安装教程:https://www.cnblogs.com/zhaomeizi/p/8483597.html
- go ethereum源码分析 PartIV Transaction相关
核心数据结构: core.types.transaction.go type Transaction struct { data txdata // caches hash atomic.Value ...
- 聊聊大学期间的我是怎样学习Linux系统的
高考成绩并不是那么的理想,本科是个普通的二本院校,来到学校之后,整个人其实很迷茫,当时对大学的专业真的是一点都不了解,也不知道自己对哪方面感兴趣,最后选择的专业是电子方面的,其实当时选择专业的时候对电 ...
- sed命令总结-Linux
sed命令总结-Linux linuxsed 2018年02月08日 19时27分57秒 命令语法经常忘记,每次总是看笔记不切实际,记不起来的要多查manual,本次总结按照manual总结,希望下次 ...
- 无聊的js(马赛克)
<!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...
- linux下jdk8安装
--- 解压命令不管用 添加插件 yum install tar --- 上传命令不管用 添加插件 wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...
- winfrom窗体中嵌套WPF控件
前言 本文主要介绍如何在winfrom窗体中嵌套WPF控件, 一来是自己记录一下,而来希望能对有需要的朋友提供实现思路. 如有错误请指出...下面进入正题... -1.前期准备 准备一个建立好的win ...
- erlang并发编程(二)
补充-------erlang并发编程 Pid =spawn(fun()-> do_sth() end). 进程监视: Ref = monitor(process, Pid)靠抛异常来终结进程 ...