用于封装数据库数据的JavaBean(实体类)

本文详细介绍了JavaBean标准类的概念,以及其实现细节。JavaBean作为实体类,用于封装数据,与数据库表一一对应。文章强调了JavaBean的四个基本要求,并通过User类实例展示了如何创建符合规范的JavaBean。

JavaBean:标准的Java类

实体类是与数据库表一一对应

  1. 要求:

    1. 类必须被public修饰
    2. 必须提供空参的构造器
    3. 成员变量必须使用private修饰
    4. 提供公共setter和getter方法
    
  2. 功能:封装数据

import java.io.Serializable;
//implements Serializable 操作dbutil时需要实现序列化
public class User implements Serializable {
    private int uid;
    private String uname;
    private String upwd;
    private String usex;
    private String uhobbey;
    private String utext;

    public User() {
    }

    public User(int uid, String uname, String upwd, String usex, String uhobbey, String utext) {
        this.uid = uid;
        this.uname = uname;
        this.upwd = upwd;
        this.usex = usex;
        this.uhobbey = uhobbey;
        this.utext = utext;
    }

    public User(String uname, String upwd, String usex, String uhobbey, String utext) {
        this.uname = uname;
        this.upwd = upwd;
        this.usex = usex;
        this.uhobbey = uhobbey;
        this.utext = utext;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String getUsex() {
        return usex;
    }

    public void setUsex(String usex) {
        this.usex = usex;
    }

    public String getUhobbey() {
        return uhobbey;
    }

    public void setUhobbey(String uhobbey) {
        this.uhobbey = uhobbey;
    }

    public String getUtext() {
        return utext;
    }

    public void setUtext(String utext) {
        this.utext = utext;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", upwd='" + upwd + '\'' +
                ", usex='" + usex + '\'' +
                ", uhobbey='" + uhobbey + '\'' +
                ", utext='" + utext + '\'' +
                '}';
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值