如何使用BeanUtils.copyProperties進(jìn)行對象之間的屬性賦值
1、使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對象之間屬性的賦值,避免通過get、set方法一個一個屬性的賦值
/**
* 對象屬性拷貝 <br>
* 將源對象的屬性拷貝到目標(biāo)對象
*
* @param source 源對象
* @param target 目標(biāo)對象
*/
public static void copyProperties(Object source, Object target) {
try {
BeanUtils.copyProperties(source, target);
} catch (BeansException e) {
LOGGER.error("BeanUtil property copy failed :BeansException", e);
} catch (Exception e) {
LOGGER.error("BeanUtil property copy failed:Exception", e);
}
}
2、List集合之間的對象屬性賦值
/**
* @param input 輸入集合
* @param clzz 輸出集合類型
* @param <E> 輸入集合類型
* @param <T> 輸出集合類型
* @return 返回集合
*/
public static <E, T> List<T> convertList2List(List<E> input, Class<T> clzz) {
List<T> output = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(input)) {
for (E source : input) {
T target = BeanUtils.instantiate(clzz);
BeanUtil.copyProperties(source, target);
output.add(target);
}
}
return output;
}
比如有兩個類,User和Employee,將存儲Employee對象的List賦給存儲User對象的List。
User類:
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Employee類:
public class Employee {
private String name;
private Integer age;
private String dept;
public Employee(String name, Integer age, String dept) {
this.name = name;
this.age = age;
this.dept = dept;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", dept='" + dept + '\'' +
'}';
}
}
測試類:
@RunWith(PowerMockRunner.class)
public class TestUtil
{
@Test
public void test(){
Employee ee1=new Employee("A",21,"it");
Employee ee2=new Employee("B",23,"account");
User user=new User();
BeanUtil.copyProperties(ee1, user);
System.out.println(user);
System.out.println("-------------分割線--------------");
List<User> output=new ArrayList<>();
List<Employee> source= Arrays.asList(ee1,ee2);
output=BeanUtil.convertList2List(source,User.class);
for (User str:output) {
System.out.println(str);
}
}
}

到此這篇關(guān)于使用BeanUtils.copyProperties進(jìn)行對象之間的屬性賦值的文章就介紹到這了,更多相關(guān)BeanUtils.copyProperties對象賦值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中YYYY-MM-dd與yyyy-MM-dd的區(qū)別及跨年問題
YYYY-MM-dd可能會導(dǎo)致跨年周的日期被歸屬到錯誤的年份, yyyy-MM-dd總是表示實際的日歷年份,無論日期所在的周是否跨年,本文就來介紹一下兩者的區(qū)別,感興趣的可以了解一下2024-01-01
使用MyBatis查詢千萬級數(shù)據(jù)量操作實現(xiàn)
這篇文章主要為大家介紹了如何使用MyBatis?查詢千萬數(shù)據(jù)量的操作過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java文件處理之使用XWPFDocument導(dǎo)出Word文檔
最近因項目開發(fā)的需要,整理了一份用JAVA導(dǎo)出WORD文檔,下面這篇文章主要給大家介紹了關(guān)于Java文件處理之使用XWPFDocument導(dǎo)出Word文檔的相關(guān)資料,需要的朋友可以參考下2023-12-12
Java開發(fā)中常用的 Websocket 技術(shù)參考
WebSocket 使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),當(dāng)然也支持客戶端發(fā)送數(shù)據(jù)到服務(wù)端。2020-09-09
Java中import java.util.Scanner的用處詳解
文章主要介紹Java中的Scanner類及其常用方法next()和nextLine()的區(qū)別,next()方法在遇到空格、Tab鍵、回車鍵等分隔符時結(jié)束輸入,而nextLine()方法則接收所有輸入,直到遇到回車鍵2024-11-11

