如何在 Spring 引导中加入 SQL 表?
How to join SQL tables in Spring Boot?
如何将 class 作者的 author_firstName 和 author_lastName 绑定到 class 本书?我使用 h2 数据库,author_id 和 book_id 是主键,我使用 postman
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private int id;
@Column(name = "book_title")
private String title;
@Column(name = "book_genre")
private String genre;
@Column(name = "author")
private String author;
}
@Entity
@Table(name="Author")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="author_id")
private int authorId;
@Column(name="author_firstName")
private String firstName;
@Column(name="author_lastName")
private String lastName;
@Column(name="author_birth")
private Date birth;
}
不是将字符串参数用于 author
,您应该将其转换为 @ManyToOne 关系:
@Entity
@Table(name="Author")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="author_id")
private int authorId;
@Column(name="author_firstName")
private String firstName;
@Column(name="author_lastName")
private String lastName;
@Column(name="author_birth")
private Date birth;
@OneToMany(mappedBy = "author")
private List<Book> books;
}
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private int id;
@Column(name = "book_title")
private String title;
@Column(name = "book_genre")
private String genre;
@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
}
使用此模型,您将能够从任何入口点、作者的书籍(特定作者撰写的书籍)或拥有书籍的作者(作者撰写的书籍集合)获取数据。
您可以使用 findAll
Spring 数据 JPA 实现的默认实现(如果您使用 EAGER 提取类型,将自动连接两个表),或者构建您自己的 JPQL:
FROM Author a FETCH JOIN a.books b WHERE a.id = b.author
.
这将有助于代码的易读性并提高数据库数据的规范化。
如何将 class 作者的 author_firstName 和 author_lastName 绑定到 class 本书?我使用 h2 数据库,author_id 和 book_id 是主键,我使用 postman
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private int id;
@Column(name = "book_title")
private String title;
@Column(name = "book_genre")
private String genre;
@Column(name = "author")
private String author;
}
@Entity
@Table(name="Author")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="author_id")
private int authorId;
@Column(name="author_firstName")
private String firstName;
@Column(name="author_lastName")
private String lastName;
@Column(name="author_birth")
private Date birth;
}
不是将字符串参数用于 author
,您应该将其转换为 @ManyToOne 关系:
@Entity
@Table(name="Author")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="author_id")
private int authorId;
@Column(name="author_firstName")
private String firstName;
@Column(name="author_lastName")
private String lastName;
@Column(name="author_birth")
private Date birth;
@OneToMany(mappedBy = "author")
private List<Book> books;
}
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private int id;
@Column(name = "book_title")
private String title;
@Column(name = "book_genre")
private String genre;
@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
}
使用此模型,您将能够从任何入口点、作者的书籍(特定作者撰写的书籍)或拥有书籍的作者(作者撰写的书籍集合)获取数据。
您可以使用 findAll
Spring 数据 JPA 实现的默认实现(如果您使用 EAGER 提取类型,将自动连接两个表),或者构建您自己的 JPQL:
FROM Author a FETCH JOIN a.books b WHERE a.id = b.author
.
这将有助于代码的易读性并提高数据库数据的规范化。