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