iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
Modern Web

Spring Boot and React - 前後端 30 天分手日記系列 第 7

Day 7 - Spring Data JPA 寫入與讀取資料 (2)

  • 分享至 

  • xImage
  •  

Day 6 - Spring Data JPA 寫入與讀取資料

上一章介紹了Spring Data JPA的使用方法

接下來讓我們把相關Model補完,

在Model資料夾下新增Skill.java

@Entity
public class Skill {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long skillId;
    private String name;
    private String type;

    public Long getSkillId() {
        return skillId;
    }

    public void setSkillId(Long skillId) {
        this.skillId = skillId;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

新增Company.java

@Entity
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long companyId;
    private String name;

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

新增Certificate.java

@Entity
public class Certificate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long certificateId;
    private String name;
    @ManyToOne
    @JoinColumn(name = "skillId")
    private Skill skill;
    @ManyToOne
    @JoinColumn(name = "companyId")
    private Company company;

    public Long getCertificateId() {
        return certificateId;
    }

    public void setCertificateId(Long certificateId) {
        this.certificateId = certificateId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

}

新增Vendor.java

@Entity
public class Vendor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long vendorId;
    private String name;

    public Long getVendorId() {
        return vendorId;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

新增ExamInfo.java

@Entity
public class Vendor {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long vendorId;
    private String name;

    public Long getVendorId() {
        return vendorId;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

新增MemberExam.java

@Entity
public class MemberExam {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long memberExamId;
    @ManyToOne
    @JoinColumn(name = "mid")
    private Member member;
    @ManyToOne
    @JoinColumn(name = "examId")
    private ExamInfo examInfo;
    private Date joinTime;

    public Long getMemberExamId() {
        return memberExamId;
    }

    public void setMemberExamId(Long memberExamId) {
        this.memberExamId = memberExamId;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public ExamInfo getExamInfo() {
        return examInfo;
    }

    public void setExamInfo(ExamInfo examInfo) {
        this.examInfo = examInfo;
    }

    public Date getJoinTime() {
    return joinTime;
    }

    public void setJoinTime(Date joinTime) {
        this.joinTime = joinTime;
    }
}

新增MemberSkill.java

@Entity
public class MemberSkill {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long memberSkillId;
    @ManyToOne
    @JoinColumn(name = "mid")
    private Member member;
    @ManyToOne
    @JoinColumn(name = "skillId")
    private Skill skill;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }

}

簡單講一下我們Model用到的Annotation

@Entity - 聲明為實體類, ORM映射對象

@Id - 聲明為主鍵

@GeneratedValue(strategy = GenerationType.IDENTITY) - 自動產生流水號

@OneToMany - 聲明關係為一對多

@ManyToOne - 聲明關係為多對一

@JoinColumn - 多對一或一對多時, 要對應到的欄位

下一章 Spring Data JPA 寫入與讀取資料 (3)


上一篇
Day 6 - Spring Data JPA 寫入與讀取資料
下一篇
Day 8 - Spring Data JPA 寫入與讀取資料 (3)
系列文
Spring Boot and React - 前後端 30 天分手日記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
joshwalker
iT邦新手 5 級 ‧ 2020-02-02 12:42:08

ExamInfo.java 的內容是錯的喔. 貼到Vendor.java 去了

0
joshwalker
iT邦新手 5 級 ‧ 2020-02-02 12:53:14

MemberSkill.java 裡面的:

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

是不是應該改為:

public Long getMemberSkillId() {
    return memberSkillId;
}

public void setMemberSkillId(Long id) {
    this.memberSkillId = id;
}
1
bhtsai
iT邦新手 5 級 ‧ 2020-10-11 12:06:40

程式片段貼錯了不改, 看起來也沒github連結給讀者自己看
為了鐵人賽而鐵人賽
不知道是要讀者們學什麼

0
coderrrr
iT邦新手 5 級 ‧ 2021-09-18 15:09:13

其實Get Set 到文的中間 引用了Lombok
那些錯誤代碼也是會被刪除
所以大家真的和我一樣要學Spring Boot的~
就繼續看下去就對~

我要留言

立即登入留言