在MVC的开发模式中经常需要将model与pojo的数据绑定,apache和spring的⼯具包中都有BeanUtils,使⽤其中的copyProperties⽅法可以⾮常⽅便的进⾏这些⼯作,但在实际应⽤中发现,对于null的处理不太符合个⼈的需要,例如在进⾏修改操作中只需要对model中某⼀项进⾏修改,那么⼀般我们在页⾯上只提交model的ID及需要修改项的值,这个时候使⽤pyProperties会将其他的null绑定到pojo中去。
org.springframework.beans.BeanUtils;
直接上代码:
st;
import com.hourumiyue.system.SpringUtil;
import org.springframework.beans.BeanUtils;
public class TestBeanUtiles {
</span><span >public</span> <span >static</span> <span >void</span><span > main(String[] args) {
NewPerson newPerson </span>= <span >new</span><span > NewPerson();
newPerson.setName(</span>"miyue");<span >//</span><span >前台⽤户更新过的数据,例如前台只修改了⽤户名
</span><span >//</span><span >下⾯我们假设是从数据库加载出来的⽼数据</span>
OldPerson oldPerson = <span >new</span><span > OldPerson();
oldPerson.setSex(</span>"nv"<span >);
oldPerson.setAge(</span>5<span >);
</span><span >//</span><span >如果我们想把新数据更新到⽼数据这个对象⾥⾯,我们就可以借助pyProperties()的⽅法如下:</span>
System.out.String());
System.out.String());
}
}
上⾯的代码打印结果如下:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='null', age=0}
从上⾯我们可以看出,新的对象确实把修改的数据更新给⽼对象了,但是它却把原来为null或者int类型默认为0的值也都赋给了⽼对象,这肯定不合理的,下⾯我们⾃⼰写了⼀个加⼯类,⼤家可以直接调⽤我们加⼯类的copyPropertiesIgnoreNull()⽅法即可忽略null值,避免⽼数据被null覆盖的尴尬:
/**
* 版权所有 (c) 2018,中⾦⽀付有限公司
*/
package com.yanshemiyue.system;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import t.ApplicationContext;
import t.ApplicationContextAware;
import java.util.HashSet;
import java.util.Set;
/
**
类说明
<pre>
Modify Information:
Author Date Description
============ =========== ============================
houru 2018年1⽉4⽇ Create this file
</pre>
*/
public class SpringUtil implements ApplicationContextAware {
</span><span >/**</span><span >
* 当前IOC
*
</span><span >*/</span>
<span >private</span> <span >static</span><span > ApplicationContext applicationContext;
</span><span >/**</span><span >
* * 设置当前上下⽂环境,此⽅法由spring⾃动装配
*
</span><span >*/</span><span >
@Override
</span><span >public</span> <span >void</span><span > setApplicationContext(ApplicationContext arg0)
</span><span >throws</span><span > BeansException {
applicationContext </span>=<span > arg0;
}
</span><span >/**</span><span >
* 从当前IOC获取bean
*
* </span><span >@param</span><span > id
* bean的id
* </span><span >@return</span><span >
*
</span><span >*/</span>
<span >public</span> <span >static</span><span > Object getObject(String id) {
Object object </span>= <span >null</span><span >;
object </span>=<span > Bean(id);
</span><span >return</span><span > object;
}
</span><span >public</span> <span >static</span><span > String[] getNullPropertyNames (Object source) {
</span><span >final</span> BeanWrapper src = <span >new</span><span > BeanWrapperImpl(source);
springframework包java.beans.PropertyDescriptor[] pds </span>=<span > PropertyDescriptors();
Set</span><String> emptyNames = <span >new</span> HashSet<String><span >();
</span><span >for</span><span >(java.beans.PropertyDescriptor pd : pds) {
Object srcValue </span>=<span > Name());
</span><span >if</span> (srcValue == <span >null</span><span >) emptyNames.Name());
}
String[] result </span>= <span >new</span><span > String[emptyNames.size()];
</span><span >return</span><span > Array(result);
}
</span><span >public</span> <span >static</span> <span >void</span><span > copyPropertiesIgnoreNull(Object src, Object target){ pyProperties(src, target, getNullPropertyNames(src));
}
}
调⽤:copyPropertiesIgnoreNull
st;
jr.system.SpringUtil;
public class TestBeanUtiles {
public static void main(String[] args) {
NewPerson newPerson = new NewPerson();
newPerson.setName("miyue");//前台⽤户更新过的数据,例如前台只修改了⽤户名
//下⾯我们假设是从数据库加载出来的⽼数据
OldPerson oldPerson = new OldPerson();
oldPerson.setSex("nv");
oldPerson.setAge(5);
//如果我们想把新数据更新到⽼数据这个对象⾥⾯,我们就可以借助pyProperties()的⽅法如下:
//pyProperties(newPerson, oldPerson);
System.out.String());
System.out.String());
}
}
打印结果:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='nv', age=0}
现在就可以看出⽼数据没有被null覆盖
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论