zoukankan      html  css  js  c++  java
  • 对象拷贝工具 Apache 的BeanUtils 和 Spring 的 BeanUtils

     导入不同的包结果完全不一样

     Spring 的 BeanUtils  (推荐使用)

    前一个内容 复制到 后一个  

    Apache 的 BeanUtils (性能差 不推荐使用)

    后一个内容 复制到 前一个

     

    public class Student {
    
        /** 学号 */
        private long id;
    
        private String name;
    
        private int age;
    
        /** 年级 */
        private int grade;
    
        /** 专业 */
        private String major;
    
        /** 学校 */
        private String school;
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public int getGrade() {
            return grade;
        }
    
        public void setGrade(int grade) {
            this.grade = grade;
        }
    
        public String getMajor() {
            return major;
        }
    
        public void setMajor(String major) {
            this.major = major;
        }
    
        public String getSchool() {
            return school;
        }
    
        public void setSchool(String school) {
            this.school = school;
        }
    
        public Student(long id, String name, int age, int grade, String major, String school) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
            this.grade = grade;
            this.major = major;
            this.school = school;
        }
    
        @Override
        public String toString() {
            return String.format("Student [id=%s, name=%s, age=%s, grade=%s, major=%s, school=%s]", id, name, age, grade,
                    major, school);
        }
    
        
    }
    Student
    import java.lang.reflect.InvocationTargetException;
    
    import org.springframework.beans.BeanUtils;
    
    //import org.apache.commons.beanutils.BeanUtils;
    
    public class Test {
        public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
    
            Student studentA = new Student(200123, "苏明", 20, 1, "土木工程", null);
            Student studentB = new Student(200789, "张阿凡", 20, 3, "计算机工程", "苏州工业大学");
            
            //后一个内容 复制到 前一个
            //用studentB 替换 studentA的字段内容
            BeanUtils.copyProperties(studentA, studentB);
            //结果都是后一个内容
            System.out.println(studentA);
            System.out.println(studentB);
            
        }
    }

    性能对比 https://www.jianshu.com/p/bcbacab3b89e 

    古人学问无遗力,少壮工夫老始成。 纸上得来终觉浅,绝知此事要躬行。
  • 相关阅读:
    Python——DataFrame中pivot()函数解析
    123
    Python中merge()函数 ,join()函数,concat()函数的区别
    加密算法------DES加密算法详解
    关于MD5的介绍
    Python中使用pandas保存dataframe
    Python中DATAFRAME多条件选择
    Excel统一表格的格式(CLEAN()函数,TRIM()函数,SUBSTITUTE()函数)
    Excel中REPLACE函数的使用方法
    VS+QT在QT designer中添加了新的控件,VS 的cpp文件没有显示对象名
  • 原文地址:https://www.cnblogs.com/wf-zhang/p/12126447.html
Copyright © 2011-2022 走看看