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的更多相关文章

  1. java 数据脱敏

    所谓数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护.在涉及客户安全数据或者一些商业性敏感数据的情况下,在不违反系统规则条件下,对真实数据进行改造并提供测试使用,如身份 ...

  2. BeanUtils 如何拷贝 List?

    BeanUtils 如何拷贝 List? 一.背景 我们在DO.Model.VO层数据间可能经常转换数据: Entity对应的是持久层数据结构(一般是数据库表的映射模型); Model 对应的是业务层 ...

  3. 【Springboot】FastJson与Jackson全局序列化方式的配置和相关工具类

    springboot 版本: <parent> <groupId>org.springframework.boot</groupId> <artifactId ...

随机推荐

  1. html页面禁止用户右键粘贴复制保存的代码

    HTML页面禁止选择.页面禁止复制.页面禁止右键 原创古城寨主2018-03-01 17:50:59评论(1)228人阅读   HTML页面内容禁止选择.复制.右键 刚在一个看一个站点的源代码的的时候 ...

  2. Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load

    Oracle 11g 单实例到单实例OGG同步实施文档-OGG initial load 2018-06-07 00:514730原创GoldenGate 作者: leo 本文链接:https://w ...

  3. [Leetcode easy]存些水题34、20、700

    leetcode 34 最早出现和最后出现 class Solution { public int[] searchRange(int[] nums, int target) { int []ans= ...

  4. 工作VUE布局记录

    以这个页面为例子   这个是你的布局有多少行,这个页面有两行,这里是2       这里span是占用多少格(一共24格)offset左右偏移,这个基本上用不到row是表示这个控件在第几行,如果是在第 ...

  5. python基础数据类型练习2

    1,写代码,有如下列表,按照要求实现每一个功能li = ['alex', 'wusir', 'eric', 'rain', 'alex'] 计算列表的长度并输出print(len(li))答:结果为5 ...

  6. ESP32搭建1.VMware Workstation 12.5下Ubuntu16.04环境搭建(简易搭建)

    一.需要下载的资源: 1.     下载VMware Workstation 链接: https://pan.baidu.com/s/1nuDEc3n 密码: 89xc 2.     下载Ubuntu ...

  7. Python序列的一点用法

    #python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 序列,顾名思义,是一段数据的有序排列,列表,元组,字符串都是序列的一种,序列有很多BIF(BIF是内建方法,即python ...

  8. Problem 2: Even Fibonacci numbers

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting w ...

  9. Qt5.7.0移植到4412

    搞了几天,终于把Qt5.7移植到4412上,下面是一些自己移植过程中的记录,希望能帮到其他人. 一.需要下载的文件, 下载qt5.7源码qt-everywhere-opensource-src-5.7 ...

  10. linux部署小结

    一.连接外网1.配置网卡 vi /etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO=static IPADDR= PREFIX= GATEWAY= ...