commons-beanutils的小例子

注意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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值