一、创建JPA项目
1、首先先创建Spring Boot项目JPADemo然后完成项目的初始化工作
2、创建评论实体类 - Comment
package net.hy.lesson07.bean;
import javax.persistence.*;
/**
* 功能:评论实体类
* 作者:黄运
* 日期:2021年05月12日
*/
@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 +
'}';
}
}
3、创建文章实体类 - Article
package net.hy.lesson07.bean;
import javax.persistence.*;
import java.util.List;
/**
* 功能:文章实体类
* 作者:黄运
* 日期:2021年05月12日
*/
@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 +
'}';
}
}
4、创建自定义JpaRepository接口 - ArticleRepository
package net.hy.lesson07.repository;
import net.hy.lesson07.bean.Article;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* 功能:文章仓库接口
* 作者:黄运
* 日期:2020年08月11日
*/
public interface ArticleRepository extends JpaRepository<Article, Integer> {
}
二、添加数据依赖配置其相关属性
1、在pom.xml文件里添加阿里巴巴数据源
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
2、在全局配置文件里配置mysql数据库连接属性
3、编写测试方法
-
打开JpaDemoApplicationTests测试类,并且注入文章仓库 - ArticleRepository
-
创建测试方法testFindAll(),并运行
@Test //测试查询全部记录
public void testFindAll(){
//查询全部文章记录
List<Article> articles = articleRepository.findAll();
//遍历输出文章列表
articles.forEach(article -> System.out.println(article));
}