发布于2021-05-29 22:31 阅读(875) 评论(0) 点赞(21) 收藏(1)
如想了解更多的相关资讯,可通过查询官网了解
设置项目数据
添加项目依赖
完成初始化工作
package net.nell.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 +
'}';
}
}
package net.nell.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 +
'}';
}
}
创建文章仓库接口ArticleRepository
package net.nell.lesson07.repository;
import net.hw.lesson07.bean.Article;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ArticleRepository extends JpaRepository<Article, Integer> {
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
# 配置数据源
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
@Test //测试查询全部记录
public void testFindAll(){
//查询全部文章记录
List<Article> articles = articleRepository.findAll();
//遍历输出文章记录
articles.forEach(article -> System.out.println(article));
}
运行程序,查看结果
package net.nell.lesson07.repository;
import net.nell.lesson07.bean.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CommentRepository extends JpaRepository<Comment, Integer> {
}
/**
* 根据文章ID进行分页查询评论
* nativeQuery = true, 表示使用原生SQL语句,否则使用HQL语句
* @param aId 查询条件字段(文章编号)
* @param pageable 可分页对象,分页查询需要该参数
* @return 返回page对象,包含page的相关信息及查询结果集
*/
@Query(value = "select * from t_comment where a_id = ?1", nativeQuery = true)
Page<Comment> findCommentPagedByArticleId01(Integer aId, Pageable pageable);
/**
* 根据文章ID进行分页查询评论
* 没有设置nativeQuery,默认就是false,表示使用HQL语句
* @param aId 查询条件字段(文章编号)
* @param pageable 可分页对象,分页查询需要该参数
* @return 返回page对象,包含page的相关信息及查询结果集
*/
@Query(value = "select c from t_comment c where c.aId = ?1")
Page<Comment> findCommentPagedByArticleId02(Integer aId, Pageable pageable);
添加测试注解,注入评论仓库
设置pageIndex = 0,表明当前页为第1页
设置pageSize = 2,表明每页最多两条记录
package net.nell.lesson07;
import net.nell.lesson07.bean.Comment;
import net.nell.lesson07.repository.CommentRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.List;
@SpringBootTest
public class CommentTests {
@Autowired // 注入评论仓库
private CommentRepository commentRepository;
@Test // 测试按文章编号分页查询评论,采用原生SQL语句
public void testFindCommentPagedByArticleId01() {
// 当前页面索引
int pageIndex = 0; // 当前页 - 第1页
// 设置页面大小
int pageSize = 2; // 每页最多2条记录
// 创建分页器
Pageable pageable = PageRequest.of(pageIndex, pageSize);
// 查询文章编号为1的页面对象
Page<Comment> page = commentRepository.findCommentPagedByArticleId01(1, pageable);
// 获取页面对象里的评论列表
List<Comment> comments = page.getContent();
// 获取总页数
int totalPages = page.getTotalPages();
// 输出页面信息
System.out.println("当前页:" + (pageIndex + 1));
System.out.println("总页数:" + totalPages);
// 输出当前页全部评论
comments.forEach(comment -> System.out.println(comment));
}
}
运行程序,查看结果
将修改页索引值为1,即可显示第2页评论
运行程序,查看结果
使每页评论按照评论编号降序排列
Sort.Direction.DESC - 降序;Sort.Direction.ASC - 升序
运行程序,查看结果
运行程序,查看结果
运行程序,查看结果
通过查看navicat中的数据确认是否更新成功
运行测试方法,查看结果
查看数据库中数据
进行操作后,在navicat中进行恢复数据的操作
在测试类CommentTests里创建测试方法testUpdateAuthorByArticleId02()
运行测试方法。查看结果
在Navicat里打开评论表,看看文章编号为1的评论作者是否都改变
编写测试方法
运行测试方法,并查看结果
在Navicat里打开评论表,看看作者为“洁柔”的评论是否被删除了。
然后,再次恢复数据表如下图所示
原文链接:https://blog.csdn.net/weixin_46705517/article/details/117282012
作者:举起你的手来
链接:http://www.javaheidong.com/blog/article/207564/b536bcff7bbdaad05de4/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!