一、简介
在JavaEE中,Java类的属性通过 getter,setter 来定义:get(或set)方法,去除get(或set)后,后字母小写即为Java类的属性。
一般情况下,字段名和属性名都一致。
beanutils 工具包就是用来操作 Java 类的属性的
二、操作步骤
1.导jar包(2个)
2.代码测试
1)创建 Student 类属性名与数据库中 Student 表中的字段名一致
package com.jdbc.study;
/**
* @author LIXICHEN
* @create 2020 -07 ${time}
*/
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 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 + "]";
}
public Student(int flowID, int type, String idCard, String examCard, String studentName, String location,
int grade) {
super();
this.flowID = flowID;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
2)进行代码测试
@Test
public void testSetProperty() throws Exception {
Object object = new Student();
System.out.println(object);
//设置属性值
BeanUtils.setProperty(object, "idCard", "21121196506091857");
System.out.println(object);
//获取属性值
Object val = BeanUtils.getProperty(object, "idCard");
System.out.println(val);
}
3)测试结果截图