为什么使用BeanUtils操做ios 属性传值 为空跑出来是空值

标签:至少1个,最多5个
最近在写一个小玩意的时候,需要在两个对象之间拷贝属性
BeanUtils.copyProperties
可是,有一个问题
就是当src对象的键值为Null时
就会把target对象的对应键值覆盖成空了
所以找了下面的这个方式来解决
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set&String& emptyNames = new HashSet&String&();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
public static void copyPropertiesIgnoreNull(Object src, Object target){
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
0 收藏&&|&&1
你可能感兴趣的文章
2 收藏,935
分享到微博?
你好!看起来你挺喜欢这个内容,但是你还没有注册帐号。 当你创建了帐号,我们能准确地追踪你关注的问题,在有新答案或内容的时候收到网页和邮件通知。还能直接向作者咨询更多细节。如果上面的内容有帮助,记得点赞 (????)? 表示感谢。
明天提醒我
我要该,理由是:
扫扫下载 AppNoteEntity entity = new NoteEntity();
entity.setNote001("a1");
entity.setNote002("a2");
entity.setNote003("a3");
java.lang.reflect.Field[] fields = entity.getClass().getDeclaredFields();
for (int i = 0; i & fields. i++) {
String name = fields[i].getName();
if(PropertyUtils.isReadable(entity, name) && PropertyUtils.isWriteable(entity, name)) {
System.out.println(name + "___" + BeanUtils.getProperty(entity, name));
阅读(...) 评论()BeanUtils操作Bean的Date属性_beanutils bean转map_词汇网
<meta name="keywords" content="BeanUtils操作Bean的Date属性beanutils bean转map,
代码片段(1)[全屏查看所有代码]
BeanUtils操作Bean的Date属性
责任编辑:词汇网 发表时间: 2:06:54
代码片段(1)[全屏查看所有代码] 1.[代码][Java]代码
Map map=new HashMap(); map.put("name","gyk" ); map.put("birthday",""); ConvertUtils.register(new DateLocaleConverter(Locale.SIMPLIFIED_CHINESE,"yyyy-MM-dd"), Date.class); Person p=new Person(); BeanUtils.populate(p, map); System.out.println(p.getBirthday());
上一集:没有了 下一集:
相关文章:&&&&&&&&
最新添加资讯
24小时热门资讯
附近好友搜索拒绝访问 | www. | 百度云加速
请打开cookies.
此网站 (www.) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(1043b3-ua98).
重新安装浏览器,或使用别的浏览器利用Apache BeanUtilsBean的copyProperties进行对象属性值copy
- redcoatjk's notebook - ITeye博客
博客分类:
在开发中进行遇到值对象和实体间的数据复制.
其中最常用的为Apache BeanUtilsBean的copyProperties.
本次测试发现该工具可兼容大部分数据格式,但在Java.sql.Date和java.util.Date复制时,原数据如为空将发送报错.
通过注册BeanUtilsBean的转换器,简单封装copyProperties方法.
完成效果: 调用封装后的工具类方法复制对象属性时, 遇到Date数据一律给予新对象对应属性赋值为null.
测试代码如下
1.1 实体类Student
public class Student {
private int intV
private double doubleV
private long longValue0;
private boolean booleanValue0;
private Boolean booleanValue1;
private String strintV
private java.util.Date utilDateV
private java.sql.Date sqlDateV
private Integer IntegerV
private Long LongValue1;
//getter/setter
public int getIntValue() {
return intV
public void setIntValue(int intValue) {
this.intValue = intV
public double getDoubleValue() {
return doubleV
public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleV
public String getStrintValue() {
return strintV
public void setStrintValue(String strintValue) {
this.strintValue = strintV
public java.util.Date getUtilDateValue() {
return utilDateV
public void setUtilDateValue(java.util.Date utilDateValue) {
this.utilDateValue = utilDateV
public java.sql.Date getSqlDateValue() {
return sqlDateV
public void setSqlDateValue(java.sql.Date sqlDateValue) {
this.sqlDateValue = sqlDateV
public Integer getIntegerValue() {
return IntegerV
public void setIntegerValue(Integer integerValue) {
IntegerValue = integerV
public long getLongValue0() {
return longValue0;
public void setLongValue0(long longValue0) {
this.longValue0 = longValue0;
public Long getLongValue1() {
return LongValue1;
public void setLongValue1(Long longValue1) {
LongValue1 = longValue1;
public boolean isBooleanValue0() {
return booleanValue0;
public void setBooleanValue0(boolean booleanValue0) {
this.booleanValue0 = booleanValue0;
public Boolean getBooleanValue1() {
return booleanValue1;
public void setBooleanValue1(Boolean booleanValue1) {
this.booleanValue1 = booleanValue1;
public Teacher getTeacher() {
public void setTeacher(Teacher teacher) {
this.teacher =
1.2 实体类teacher
public class Teacher {
//getter/setter
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
2 简单封装的工具类BeanUtilPlus1
package com.test.
import java.lang.reflect.InvocationTargetE
import java.util.HashM
import java.util.M
import mons.beanutils.BeanU
import mons.beanutils.ConvertU
import mons.beanutils.converters.DateC
import mons.beanutils.converters.SqlDateC
import mons.logging.L
import mons.logging.LogF
* 继承apache的BeanUtils,避免调用copyProperties时因date为空而报错的情况
public class BeanUtilPlus1 extends BeanUtils {
private static Map cache = new HashMap();
private static Log logger = LogFactory.getFactory().getInstance(
BeanUtilPlus1.class);
private BeanUtilPlus1() {
//注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
ConvertUtils.register(new SqlDateConverter(null),
java.sql.Date.class);
//ConvertUtils.register(new SqlTimestampConverter(), java.sql.Timestamp.class);
//注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
ConvertUtils.register(new DateConverter(null), java.util.Date.class);
public static void copyProperties(Object target, Object source)
throws InvocationTargetException, IllegalAccessException {
//支持对日期copy
mons.beanutils.BeanUtils.copyProperties(target, source);
3 测试类testClient
使用反射机制打印出对象的属性值
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import com.test.util.BeanUtilPlus1;
public class testClient {
* 测试BeanUtilsBean.getInstance().copyProperties对数据进行赋值时候的效果
* java.sql.Date不可为空值
* java.util.Date不可为空值
* 使用改进的BeanUtilPlus1.copyProperties
* Date为空时,直接赋空值.
public static void main(String[] args) {
* 初始化数据
Teacher t1=new Teacher();
t1.setId(1);
t1.setName("teacher t1");
Student s1=new Student();
s1.setBooleanValue0(true);
s1.setBooleanValue1(true);
s1.setDoubleValue(1.1);
s1.setIntegerValue(12);
s1.setIntValue(123);
s1.setLongValue0(12L);
s1.setLongValue1(2L);
s1.setSqlDateValue(new java.sql.Date(new java.util.Date().getTime()));
s1.setUtilDateValue(new java.util.Date());
s1.setStrintValue("student s1");
s1.setTeacher(t1);
//打印出所有内容
Class c = Class.forName("Student");
Object o = c.newInstance();
Object o=s1;
Method[] ms= c.getMethods();
System.out.println(ms.length);
System.out.println("原始数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method
"+m.getName()+"
System.out.println(m.invoke(o));
} catch (Exception e) {
e.printStackTrace();
Student s2=new Student();
BeanUtilsBean.getInstance().copyProperties(s2, s1);
//调用copy方法
BeanUtilPlus1.copyProperties(s2,s1); //调用改写过的copy方法
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
Class c = Class.forName("Student");
Object o=s2;
Method[] ms= c.getMethods();
System.out.println("新数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method
"+m.getName()+"
System.out.println(m.invoke(o));
} catch (Exception e) {
e.printStackTrace();
浏览: 3325 次
来自: 上海
1 public static Singleton getIn ...}

我要回帖

更多关于 我写的主角跑出来了 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信