<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          使用BeanUtils.copyProperties進(jìn)行對(duì)象之間的屬性賦值

          共 7407字,需瀏覽 15分鐘

           ·

          2021-08-01 16:39

          1、使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對(duì)象之間屬性的賦值,避免通過get、set方法一個(gè)一個(gè)屬性的賦值

          /**
           * 對(duì)象屬性拷貝 <br>
           * 將源對(duì)象的屬性拷貝到目標(biāo)對(duì)象
           *
           * @param source 源對(duì)象
           * @param target 目標(biāo)對(duì)象
           */

          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集合之間的對(duì)象屬性賦值

          /**
           * @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,將存儲(chǔ)Employee對(duì)象的List賦給存儲(chǔ)User對(duì)象的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 + '\'' +
                          '}';
              }
          }

          測(cè)試類:

          @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);
                  }
              }
          }

          感謝閱讀,希望對(duì)你有所幫助 :) 

          來源:blog.csdn.net/dongyuxu342719/

          article/details/90242904

          END

          最近給大家找了  通用權(quán)限系統(tǒng)


          資源,怎么領(lǐng)???


          掃二維碼,加我微信,回復(fù):通用權(quán)限系統(tǒng)

           注意,不要亂回復(fù) 

          沒錯(cuò),不是機(jī)器人
          記得一定要等待,等待才有好東西


          瀏覽 33
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  激情乱伦电影 | 欧美高清在线 | 亚洲一区小电影网站 | 麻豆成人传媒网,一区二区三区四区 | 少妇精品久久久久久久久久 |