BeanUtils.copyProperties() 用法
轉(zhuǎn)載自
https://blog.csdn.net/jdjdndhj/article/details/62056137
第一步: BeanUtils.copyProperties()與PropertyUtils.copyProperties()
1、 通過(guò)反射將一個(gè)對(duì)象的值賦值個(gè)另外一個(gè)對(duì)象(前提是對(duì)象中屬性的名字相同)。
2、 BeanUtils.copyProperties(obj1,obj2); 經(jīng)常鬧混不知道是誰(shuí)給誰(shuí)賦值,無(wú)意中先到"前面復(fù)制給后面"這個(gè)詞來(lái)幫助自己記憶這個(gè)功能。即將obj2的值賦值給obj1。
3、 如果2中實(shí)例obj2為空對(duì)象,即值new了他的實(shí)例并沒(méi)有賦值的話obj1對(duì)應(yīng)的屬性值也會(huì)被設(shè)置為空置。
4、BeanUtils與PropertyUtils對(duì)比(這里對(duì)比copyProperties方法)
PropertyUtils的copyProperties()方法幾乎與BeanUtils.copyProperties()相同,主要的區(qū)別在于后者提供類型轉(zhuǎn)換功能,即發(fā)現(xiàn)兩個(gè)JavaBean的同名屬性為不同類型時(shí),在支持的數(shù)據(jù)類型范圍內(nèi)進(jìn)行轉(zhuǎn)換,BeanUtils 不支持這個(gè)功能,但是BeanUtils速度會(huì)更快一些。
主要支持轉(zhuǎn)換類型如下:
* java.lang.BigDecimal
* java.lang.BigInteger
* boolean and java.lang.Boolean
* byte and java.lang.Byte
* char and java.lang.Character
* java.lang.Class
* double and java.lang.Double
* float and java.lang.Float
* int and java.lang.Integer
* long and java.lang.Long
* short and java.lang.Short
* java.lang.String
* java.sql.Date
* java.sql.Time
* java.sql.Timestamp
不支持java.util.Date轉(zhuǎn)換,但支持java.sql.Date。如果開(kāi)發(fā)中Date類型采用util而非sql.Date程序會(huì)拋出argument mistype異常。
第二步:擴(kuò)展BeanUtils支持時(shí)間類型轉(zhuǎn)換
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
/**
* 重寫(xiě)B(tài)eanUtils.copyProperties
*
* @author monkey
*/
public class BeanUtilsExtends extends BeanUtils {
? ?static {
? ? ? ?ConvertUtils.register(new DateConvert(), java.util.Date.class);
? ? ? ?ConvertUtils.register(new DateConvert(), java.sql.Date.class);
? ?}
? ?public static void copyProperties(Object dest, Object orig) {
? ? ? ?try {
? ? ? ? ? ?BeanUtils.copyProperties(dest, orig);
? ? ? ?} catch (IllegalAccessException ex) {
? ? ? ? ? ?ex.printStackTrace();
? ? ? ?} catch (InvocationTargetException ex) {
? ? ? ? ? ?ex.printStackTrace();
? ? ? ?}
? ?}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
/**
* 重寫(xiě)日期轉(zhuǎn)換
*
* @author houzhiqing
*/
public class DateConvert implements Converter {
? ?public Object convert(Class arg0, Object arg1) {
? ? ? ?String p = (String) arg1;
? ? ? ?if (p == null || p.trim().length() == 0) {
? ? ? ? ? ?return null;
? ? ? ?}
? ? ? ?try {
? ? ? ? ? ?SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
? ? ? ? ? ?return df.parse(p.trim());
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
? ? ? ? ? ? ? ?return df.parse(p.trim());
? ? ? ? ? ?} catch (ParseException ex) {
? ? ? ? ? ? ? ?return null;
? ? ? ? ? ?}
? ? ? ?}
? ?}
?
}
———————————————————————————————————————------------------------------
?
? ? ? 今天開(kāi)發(fā)中遇到一個(gè)問(wèn)題,其實(shí)也算不上是問(wèn)題,只是本猿比較懶而已!目前本猿主要做的是接口開(kāi)發(fā),現(xiàn)在需要將接口提供方的一個(gè)類中的部分字段挪到我自己的項(xiàng)目來(lái),然而奈何本猿太懶,不想一個(gè)一個(gè)set、get……?
要不然說(shuō)“懶”是促進(jìn)社會(huì)科技進(jìn)步的最大動(dòng)力呢!!!鑒于這一情況,本猿果斷使用了Spring神器的一個(gè)工具包——BeansUtils,簡(jiǎn)直帥出了宇宙!下面就讓本猿來(lái)帶你們見(jiàn)識(shí)見(jiàn)識(shí)這個(gè)逆天的工具。?
Spring這里不多做說(shuō)明了,簡(jiǎn)而言之言而簡(jiǎn)之就是一個(gè)大容器,至于容器中有什么東西以后有時(shí)間再細(xì)說(shuō),這個(gè)大容器呢在我們開(kāi)發(fā)中經(jīng)常用來(lái)作為一個(gè)管家管理我們的bean,既然管理bean,那我這個(gè)類到類的屬性應(yīng)該也可以管的咯!?
上demo:
這是一個(gè)類,暫且定他為遠(yuǎn)程的,
package com.fcloud.mobile.common.demo; /*** Created by Promonkey on 2017/3/14.*/ public class LongDistanceBean{/*** 用戶ID*/private String userId;/*** 居住城市*/private String liveCity;/*** 工作城市*/private String workCity;/*** 工作電話*/private String workPhone;/*** 構(gòu)造器*/public LongDistanceBean(String userId, String liveCity, String workCity, String workPhone) {this.userId = userId;this.liveCity = liveCity;this.workCity = workCity;this.workPhone= workPhone;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getLiveCity() {return liveCity;}public void setLiveCity(String liveCity) {this.liveCity = liveCity;}public String getWorkCity() {return workCity;}public void setWorkCity(String workCity) {this.workCity = workCity;}public String getWorkPhone() {return workPhone;}public void setWorkPhone(String workPhone) {this.workPhone = workPhone;}@Overridepublic String toString() {return "LongDistanceBean{" +"userId='" + userId + '\'' +", liveCity='" + liveCity + '\'' +", workCity='" + workCity + '\'' +", workPhone='" + workPhone + '\'' +'}';} }本地的類:
package com.fcloud.mobile.common.demo; /*** Created by Promonkey on 2017/3/14.*/ public class LocalBean{/*** 用戶ID*/private String userId;/*** 居住城市*/private String liveCity;/*** 工作城市*/private String workCity;/*** 工作電話*/private String workPhone;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getLiveCity() {return liveCity;}public void setLiveCity(String liveCity) {this.liveCity = liveCity;}public String getWorkCity() {return workCity;}public void setWorkCity(String workCity) {this.workCity = workCity;}public String getWorkPhone() {return workPhone;}public void setWorkPhone(String workPhone) {this.workPhone = workPhone;}@Overridepublic String toString() {return "LocalBean{" +"userId='" + userId + '\'' +", liveCity='" + liveCity + '\'' +", workCity='" + workCity + '\'' +", workPhone='" + workPhone + '\'' +'}';} }現(xiàn)在假設(shè)我調(diào)用某個(gè)接口獲得了遠(yuǎn)程端Bean的對(duì)象及他的數(shù)據(jù):
LongDistanceBeand longDistance= new LongDistanceBean("10001", "江西", "上海", "100-110-111");- 1
但是我需要用本地的一個(gè)類去封裝這些數(shù)據(jù),而且不能通過(guò)繼承(子類父類)的關(guān)系去實(shí)現(xiàn),實(shí)際開(kāi)發(fā)中經(jīng)常會(huì)遇到這種情況。怎么辦呢?別告訴我你想一個(gè)一個(gè) localBean.setUserId(longDistance.getUserId())……, 那我告訴你我要封裝的數(shù)據(jù)其實(shí)有二十幾個(gè)字段,你給我去一個(gè)一個(gè)set!?
這時(shí)候我們就需要Spring神器了:
Copy結(jié)束:?
BeanUtils.copyProperties(source, target);?
source —— 復(fù)制源?
target —— 賦值目標(biāo)
建議自己去體會(huì)一下,需要的jar包,:?
maven項(xiàng)目:
總結(jié)
以上是生活随笔為你收集整理的BeanUtils.copyProperties() 用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: idea中git分支的使用
- 下一篇: mybatis中去除多余的前缀或者后缀