JDBC技术(十)——beanutils

一、简介

在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)测试结果截图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值