1. 程式人生 > >數據庫中有外鍵時JavaBean的寫法

數據庫中有外鍵時JavaBean的寫法

tex java term 找到 color nag 出現 覆蓋 分享

當寫表和實體類之間映射的時候,一旦出現外鍵,要通過外鍵找到另外一個類,一旦出現外鍵必須寫成類型
private Address address;
person表

public class Person {
    private String pid;
    private String pname;
    private int age;

    private Address address;

    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    //覆蓋原來的toString方法
    @Override
    public String toString() {
        return "Person [pid=" + pid + ",pname=" + pname + ",age=" + age +"]";
    }
}

技術分享圖片

public class Address {
    private String aid; //主鍵
    private String province; //省
    private String city; //市
    private String district; //區
    private String street; //街道

    public String getAid() {
        return aid;
    }
    public void setAid(String aid) {
        this.aid = aid;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getDistrict() {
        return district;
    }
    public void setDistrict(String district) {
        this.district = district;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
}

數據庫中有外鍵時JavaBean的寫法