commons-beanutils的小例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiaotuwoaini/article/details/72823632

注意BeanUtils的属性赋值是通过setter方法,因此尽量使用小写字母开头的属性名字。



public class Student{
    private int flowID;//流水号
    private int type;//四六级
    private String iDCard;//身份证号
    private String examCard;//准考证号
    private String studentName;//学生姓名
    private String location;//区域
    private int grade;//学生成绩

    public Student() {
        super();
    }

    public Student(int flowID, int type, String iDCard, String examCard, String studentName, String location, int grade) {
        this.flowID = flowID;
        this.type = type;
        this.iDCard = iDCard;
        this.examCard = examCard;
        this.studentName = studentName;
        this.location = location;
        this.grade = grade;
    }

    public int getFlowID() {
        return flowID;
    }

    public void setFlowID(int flowID) {
        this.flowID = flowID;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getiDCard() {
        return iDCard;
    }

    public void setiDCard(String iDCard) {
        this.iDCard = iDCard;
    }

    public String getExamCard() {
        return examCard;
    }

    public void setExamCard(String examCard) {
        this.examCard = examCard;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "flowID=" + flowID +
                ", type=" + type +
                ", iDCard='" + iDCard + '\'' +
                ", examCard='" + examCard + '\'' +
                ", studentName='" + studentName + '\'' +
                ", location='" + location + '\'' +
                ", grade=" + grade +
                '}';
    }
}


场景类:


import org.apache.commons.beanutils.BeanUtils;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dushangkui on 2017/5/31.
 */
public class BeanUtilsTest {
    public static void main(String[] args) throws Exception {
        Map<String,Object> student = new HashMap<String, Object>();
        student.put("flowID", 1);
        student.put("type", 4);
        student.put("iDCard", "123456");
        student.put("examCard", "234567");
        student.put("studentName", "zhangsan");
        student.put("location", "玄武");
        student.put("grade", 1);

        Student stu = new Student();
        for(Map.Entry<String, Object> entry : student.entrySet()){
            BeanUtils.setProperty(stu,entry.getKey(),entry.getValue());
        }

        System.out.println(stu);
    }
}


猜你喜欢

转载自blog.csdn.net/jiaotuwoaini/article/details/72823632