Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解
copyProperties進(jìn)行對象之間的屬性賦值
1、使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對象之間屬性的賦值,避免通過get、set方法一個(gè)一個(gè)屬性的賦值
/** * 對象屬性拷貝 <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; }
比如有兩個(gè)類,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)于Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解的文章就介紹到這了,更多相關(guān)copyProperties進(jìn)行對象之間的屬性賦值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HttpClient 在Java項(xiàng)目中的使用詳解
HttpClient作為訪問Http服務(wù)的客戶端訪問程序已經(jīng)被廣泛使用,提高了開發(fā)效率,也提高了代碼的健壯性。因此熟練掌握HttpClient是必需的,關(guān)于httpclient感興趣的朋友可以參考本篇文章2015-10-10Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03基于Java實(shí)現(xiàn)計(jì)數(shù)排序,桶排序和基數(shù)排序
這篇文章主要為大家詳細(xì)介紹了計(jì)數(shù)排序,桶排序和基數(shù)排序的多種語言的實(shí)現(xiàn)(JavaScript、Python、Go語言、Java),感興趣的小伙伴可以了解一下2022-12-12Idea創(chuàng)建springboot不能選擇java8的解決
在IDEA 2023版本創(chuàng)建Spring Boot項(xiàng)目時(shí),發(fā)現(xiàn)沒有Java 8選項(xiàng),只有Java 17和Java 20,解決方法包括:通過修改服務(wù)器URL(推薦)或直接在創(chuàng)建后修改pom.xml文件中的Spring Boot和Java版本2025-01-01Java使用String類格式化當(dāng)前日期實(shí)現(xiàn)代碼
這篇文章主要介紹了Java使用String類格式化當(dāng)前日期實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02解決springboot服務(wù)啟動報(bào)錯(cuò):Unable?to?start?embedded?contain
這篇文章主要介紹了解決springboot服務(wù)啟動報(bào)錯(cuò):Unable?to?start?embedded?contain的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08