1.创建项目
1.1创建评论实体类 - Comment
package net.jyq.lesson07.bean;
import javax.persistence.*;
@Entity(name = "t_comment")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "content")
private String content;
@Column(name = "author")
private String author;
@Column(name = "a_id")
private Integer aId;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
}
1.2创建文章类
package net.jyq.lesson07.bean;
import javax.persistence.*;
import java.util.List;
@Entity(name = "t_article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "title")
private String title;
@Column(name = "content")
private String content;
// 查询时将子表一并查询出来
@OneToMany(fetch = FetchType.EAGER) // FetchType.LAZY 懒加载
@JoinTable(name = "t_comment", joinColumns = {@JoinColumn(name = "a_id")},
inverseJoinColumns = {@JoinColumn(name = "id")})
private List<Comment> commentList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Comment> getCommentList() {
return commentList;
}
public void setCommentList(List<Comment> commentList) {
this.commentList = commentList;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", commentList=" + commentList +
'}';
}
}
1.3创建文章仓库接口ArticleRepository
package net.jyq.lesson07.repository;
import net.nell.lesson07.bean.Article;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ArticleRepository extends JpaRepository<Article,Integer> {
}
1.4添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
1.5在全局配置文件配置数据源
# 配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/blog?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=p@ssw0rd
spring.datasource.druid.max-active=100
spring.datasource.druid.min-idle=10
spring.datasource.druid.initial-size=20
2.在测试类里编写测试方法
注入仓库
2.1创建测试方法testfindall()
package net.nell.lesson07;
import net.nell.lesson07.bean.Article;
import net.nell.lesson07.repository.ArticleRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class JpaDemoApplicationTests {
@Autowired //注入文章仓库接口
private ArticleRepository articleRepository;
@Test
void contextLoads() {
}
@Test //测试查询全部记录
public void testFindAll(){
//查询全部文章记录
List<Article> articles = articleRepository.findAll();
//遍历输出文章记录
articles.forEach(article -> System.out.println(article));
}
}
运行结果