zoukankan      html  css  js  c++  java
  • 工具类:对象拷贝BeanUtils(提高对象拷贝效率)

    我们直接使用maven构建的项目演示:

    1. 在maven的pom.xml文件中引入BeanUtils的jar包:

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>

    2. 新建实体类Student.java

    package com.drew.entity;
    
    /**
     * @author zero 2019/03/24
     */
    public class Student {
        private Integer stuId;
        private String stuName;
    
        public Integer getStuId() {
            return stuId;
        }
    
        public void setStuId(Integer stuId) {
            this.stuId = stuId;
        }
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    
        @Override
        public String toString() {
            return "Student [stuId=" + stuId + ", stuName=" + stuName + "]";
        }
    
        /**
         * @param stuId
         * @param stuName
         */
        public Student(Integer stuId, String stuName) {
            super();
            this.stuId = stuId;
            this.stuName = stuName;
        }
    
        public Student() {
            super();
        }
    
    }
    Student.java

    3. 新建测试类:TestBeanUtils.java

     1 package com.drew.test;
     2 
     3 import java.lang.reflect.InvocationTargetException;
     4 
     5 import org.apache.commons.beanutils.BeanUtils;
     6 
     7 import com.drew.entity.Student;
     8 
     9 /**
    10  * @author zero 2019/03/24
    11  */
    12 public class TestBeanUtils {
    13     
    14     public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
    15         Student student = new Student(1, "Drew");
    16         Student student2 = (Student)BeanUtils.cloneBean(student);
    17         System.out.println(student2);
    18         
    19         Student student3 = new Student();
    20         BeanUtils.copyProperties(student3, student);// 第一个参数是:目标存储,第二个参数:源数据
    21         System.out.println(student3);
    22     }
    23 
    24 }

    3. 测试结果:

  • 相关阅读:
    VueJS中学习使用Vuex详解
    https://www.cnblogs.com/chinabin1993/p/9848720.html
    5分钟带你入门vuex(vue状态管理)
    引用第三方 chalk 模块
    Vue-Grid-Layout分享一款好用的可拖拽组件
    vue-grid-layout
    拖拽 ‘vue-grid-layout’ 插件了解下
    Vue国际化处理 vue-i18n 以及项目自动切换中英文
    Java线程池ThreadPoolExecutor使用和分析(三)
    Java线程池ThreadPoolExecutor使用和分析(二)
  • 原文地址:https://www.cnblogs.com/superdrew/p/10589123.html
Copyright © 2011-2022 走看看