溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

數(shù)據(jù)庫中有外鍵時(shí)JavaBean的寫法

發(fā)布時(shí)間:2020-08-06 20:26:17 來源:網(wǎng)絡(luò) 閱讀:3627 作者:Adam的blog 欄目:MySQL數(shù)據(jù)庫

當(dāng)寫表和實(shí)體類之間映射的時(shí)候,一旦出現(xiàn)外鍵,要通過外鍵找到另外一個(gè)類,一旦出現(xiàn)外鍵必須寫成類型
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 +"]";
    }
}

數(shù)據(jù)庫中有外鍵時(shí)JavaBean的寫法

public class Address {
    private String aid; //主鍵
    private String province; //省
    private String city; //市
    private String district; //區(qū)
    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;
    }
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI