BeanUtils.copyProperties() 用法
轉載自
https://blog.csdn.net/jdjdndhj/article/details/62056137
第一步: BeanUtils.copyProperties()與PropertyUtils.copyProperties()
1、 通過反射將一個對象的值賦值個另外一個對象(前提是對象中屬性的名字相同)。
2、 BeanUtils.copyProperties(obj1,obj2); 經常鬧混不知道是誰給誰賦值,無意中先到"前面復制給后面"這個詞來幫助自己記憶這個功能。即將obj2的值賦值給obj1。
3、 如果2中實例obj2為空對象,即值new了他的實例并沒有賦值的話obj1對應的屬性值也會被設置為空置。
4、BeanUtils與PropertyUtils對比(這里對比copyProperties方法)
PropertyUtils的copyProperties()方法幾乎與BeanUtils.copyProperties()相同,主要的區別在于后者提供類型轉換功能,即發現兩個JavaBean的同名屬性為不同類型時,在支持的數據類型范圍內進行轉換,BeanUtils 不支持這個功能,但是BeanUtils速度會更快一些。
主要支持轉換類型如下:
* 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轉換,但支持java.sql.Date。如果開發中Date類型采用util而非sql.Date程序會拋出argument mistype異常。
第二步:擴展BeanUtils支持時間類型轉換
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
/**
* 重寫BeanUtils.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;
/**
* 重寫日期轉換
*
* @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;
? ? ? ? ? ?}
? ? ? ?}
? ?}
?
}
———————————————————————————————————————------------------------------
?
? ? ? 今天開發中遇到一個問題,其實也算不上是問題,只是本猿比較懶而已!目前本猿主要做的是接口開發,現在需要將接口提供方的一個類中的部分字段挪到我自己的項目來,然而奈何本猿太懶,不想一個一個set、get……?
要不然說“懶”是促進社會科技進步的最大動力呢!!!鑒于這一情況,本猿果斷使用了Spring神器的一個工具包——BeansUtils,簡直帥出了宇宙!下面就讓本猿來帶你們見識見識這個逆天的工具。?
Spring這里不多做說明了,簡而言之言而簡之就是一個大容器,至于容器中有什么東西以后有時間再細說,這個大容器呢在我們開發中經常用來作為一個管家管理我們的bean,既然管理bean,那我這個類到類的屬性應該也可以管的咯!?
上demo:
這是一個類,暫且定他為遠程的,
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;/*** 構造器*/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 + '\'' +'}';} }現在假設我調用某個接口獲得了遠程端Bean的對象及他的數據:
LongDistanceBeand longDistance= new LongDistanceBean("10001", "江西", "上海", "100-110-111");- 1
但是我需要用本地的一個類去封裝這些數據,而且不能通過繼承(子類父類)的關系去實現,實際開發中經常會遇到這種情況。怎么辦呢?別告訴我你想一個一個 localBean.setUserId(longDistance.getUserId())……, 那我告訴你我要封裝的數據其實有二十幾個字段,你給我去一個一個set!?
這時候我們就需要Spring神器了:
Copy結束:?
BeanUtils.copyProperties(source, target);?
source —— 復制源?
target —— 賦值目標
建議自己去體會一下,需要的jar包,:?
maven項目:
總結
以上是生活随笔為你收集整理的BeanUtils.copyProperties() 用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea中git分支的使用
- 下一篇: mybatis中去除多余的前缀或者后缀