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 ...
随机推荐
- js 如何判断数组元素是否存在重复项
1.如何判断数组元素是否存在重复项 1)定义测试数组 //定义测试的数组(1个没有重复元素,1个有重复元素) var arr1 = new Array("111","33 ...
- linux图像界面连接-xdm
有两种方法可以激活 --:修改/etc/gdm/custom.conf文件在 [security]字段下增加AllowRemoteRoot=true [xdmcp]字段下增加Enable=true - ...
- leetcode 链表相关
1.给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们 ...
- 重命名文件夹提示"找不到指定文件"
本人Win10使用360粉碎了一个文件夹后,出现如下问题,并且重启无效,因已修复好,借图: 出现以上问题不用慌,程序员必备技能,重装系统可破. 哈哈,开个玩笑,解决方法步骤如下: ①百度下载 Fold ...
- 笨办法41学会说面向对象【pyinstaller安装使用
urllib库安装 先切换到pip所在目录 D:\Program Files\JetBrains\PyCharm 2017.3.3\untitled>cd /d c:\ c:\>cd c: ...
- tensorflow 经典教程及案例
导语:本文是TensorFlow实现流行机器学习算法的教程汇集,目标是让读者可以轻松通过清晰简明的案例深入了解 TensorFlow.这些案例适合那些想要实现一些 TensorFlow 案例的初学者. ...
- Ubuntu 18.04安装中文输入法
reference: https://blog.csdn.net/fx_yzjy101/article/details/80243710 https://pinyin.sogou.com/linux/ ...
- 阶段01Java基础day26反射
27.01_反射(类的加载概述和加载时机) A:类的加载概述 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载 就是指将class文 ...
- JavaScript 获得客户端IP
Below are all the free active IP lookup services I could find and the information they return. If yo ...
- shell进程中的特殊状态变量
$?:获取执行上一个指令的执行状态返回值(0为成功,非0为失败) $$:获取当前执行的shell脚本的进程号(PID) $!:获取上一个在后台工作的进程的进程号 $_:获取在此之前执行的命令或脚本的最 ...