将src非空的属性注入到des中
package lizikj.bigwheel.common.vo.merchandise.util;
import java.lang.reflect.Field;
import lizikj.bigwheel.common.vo.merchandise.Merchandise;
public class ObjUtils {
/**
* 将src非空的属性注入到des中
* @param des
* @param src
*/
public static void copyPropertysWithoutNull(Object des, Object src){
Class<?> clazz = des.getClass();
Field[] srcfields=src.getClass().getDeclaredFields();
for(Field field:srcfields){
if(field.getName().equals("serialVersionUID"))
continue;
Field f;
try {
f = clazz.getDeclaredField(field.getName());
f.setAccessible(true);
field.setAccessible(true);
Object obj = field.get(src);
if(obj!=null)
f.set(des,obj);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Merchandise origin=new Merchandise();
origin.setBarCode("2");
origin.setBrowseNum(1);
origin.setCategoryId(1);
origin.setDescribe(null);
origin.setBrand(null);
Merchandise destination=new Merchandise();
destination.setBarCode(null);
destination.setCategoryId(2);
destination.setDescribe("5555");
destination.setBrand("ttt");
ObjUtils.copyPropertysWithoutNull(destination, origin);
System.out.println(destination.getBarCode());
System.out.println(destination.getDescribe());
System.out.println(destination.getBrand());
System.out.println(destination.getCategoryId());
}
}
忘记在那里找的了
import Java.lang.reflect.Field;
public class BeanUtils {
public static void copyPropertysWithoutNull(Object des, Object src) throws Exception{
Class<?> clazz = des.getClass();
Field[] srcfields=src.getClass().getDeclaredFields();
for(Field field:srcfields){
if(field.getName().equals("serialVersionUID"))
continue;
Field f =clazz.getDeclaredField(field.getName());
field.setAccessible(true);
Object obj = field.get(src);
if(obj!=null)
f.set(des,field.get(src));
}
}
}
来自:http://blog.csdn.net/wutongyu344/article/details/7353951
将src非空的属性注入到des中的更多相关文章
- 非空校验在oracle和mysql中的用法
oracle判断是否为null nvl(参数1,参数2) :如果参数1为null则返回参数2,否则返回参数1 mysql判断是否为null ifnull(参数1,参数2) :如果参数1为null则返回 ...
- Java 遍历一个对象的属性 将非空属性赋值给另一个对象
//将origin属性注入到destination中 public <T> void mergeObject(T origin, T destination) { if (origin = ...
- SpringBoot之Spring@Value属性注入使用详解
在使用Spring框架的项目中,@Value是使用比较频繁的注解之一,它的作用是将配置文件中key对应的值赋值给它标注的属性.在日常使用中我们常用的功能都比较简单,本篇文章系统的带大家来了解一下@Va ...
- 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)
[优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...
- C# 9.0 新特性之参数非空检查简化
阅读本文大概需要 1.5 分钟. 参数非空检查是缩写类库很常见的操作,在一个方法中要求参数不能为空,否则抛出相应的异常.比如: public static string HashPassword(st ...
- spring的属性注入和构造器注入
spring在向IOC容器中注入Bean的时候,有三种注入方式: 属性注入构造器注入工厂方法注入平常中用到的前两种方法较多,下面对前两种方法举例.一.属性注入1.创建一个car类,作为注入的bean ...
- throw关键字和Objects非空判断_requireNonNull方法
作用: 可以使用throw关键字在指定的方法中抛出指定的异常 使用格式: throw new xxxException("异常产生的原因") 注意: 1.throw关键字必须写在方 ...
- 什么是静态代码块?java中如何使用空参构造方法自动生成不同名字的对象,使用非静态的属性和静态属性有什么区别,原因是什么?如何理解static关键字
静态代码块?类加载就执行,最先执行 class demo{ static int num; static{ num=10; num*=3; System.out.println("haha& ...
- 单选按钮 设置required属性无法进行非空验证
先看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
随机推荐
- Linux zip/unzip命令
From: http://www.ixdba.net/a/os/linux/2010/0725/359.html From: http://www.cnblogs.com/chinareny2k/ar ...
- Js_Ajax_输入词提示
输入"1",有提示,否则没有 suggest.jsp #suggest { background-color: #e0e0e0; width: 200px; } var xhr; ...
- 删除空文件夹 清除CS扩展名文件 bat
删除空文件夹.删的干净.删的彻底. 将下列代码复制到txt中保存.并把后缀.txt命成.bat.然后运行即可. 方案1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 三、Distributing Maya Plugins
For example, a fully implemented render utility node will have at least three files: the plug-in fil ...
- adb devices offline 问题大总结
遇到doc对话框中adb devices ,一直显示设备处于offline状态,各种搜后安装所谓的: 1.开启usb调试模式2.关闭第三方手机助手软件3.重启adb服务 adb kill-ser ...
- WinCE系统深度定制汇总
WinCE应用程序崩溃提示框的处理 WinCE6.0多国语言软键盘 定制WinCE6.0标准界面的一种方法 如何禁用WinCE下的沙漏图标 Silent RNAApp.exe for WinCE6.0 ...
- Sturts2 工作原理
上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:https://github.com/apache/strut ...
- openStack镜像制作
参考链接: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/OpenStack/page/Creating ...
- Linxu学习之03_LInux文件与目录管理
同样只介绍相关命令 这节相关主要的命令有这些: 1.目录的相关操作 cd----切换目录 pwd----显示当前目录 mkdir----新建一个新的目录 rmdir----删除一个空的目录
- MySQL日期数据类型、时间类型使用总结
MySQL日期数据类型.时间类型使用总结 MySQL日期数据类型.MySQL时间类型使用总结,需要的朋友可以参考下. MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 ...