千家信息网

springboot中怎么利用Jpa 实现分页功能

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇文章给大家分享的是有关springboot中怎么利用Jpa 实现分页功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Springb
千家信息网最后更新 2025年12月02日springboot中怎么利用Jpa 实现分页功能

本篇文章给大家分享的是有关springboot中怎么利用Jpa 实现分页功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Springboot 集成 Jpa 实现分页

由于用的技术并不复杂,所以我们开门见山,直接上代码

先来看下代码结构

pom.xml 引入相关jar包

    4.0.0            org.springframework.boot        spring-boot-starter-parent        2.3.4.RELEASE                 com.example    springboot-blog    0.0.1-SNAPSHOT    springboot-blog    Blog project for Spring Boot            1.8                            org.springframework.boot            spring-boot-starter-data-jpa                            org.springframework.boot            spring-boot-starter-thymeleaf                            org.springframework.boot            spring-boot-starter-web                            junit            junit                            mysql            mysql-connector-java            runtime                            org.projectlombok            lombok            true                            org.springframework.boot            spring-boot-starter-test            test                                                org.junit.vintage                    junit-vintage-engine                                                                                org.springframework.boot                spring-boot-maven-plugin                        

application.properties配置

# 配置数据源spring.datasource.url=jdbc:mysql://localhost:3306/blog_systemspring.datasource.username=rootspring.datasource.password=root#基本配置spring.application.name=springboot-blogserver.port=8080# 显示sqlspring.jpa.show-sql=true

实体类Article

@Data@ToString@Entity@Table(name = "t_article")public class Article {    @Id    private Integer id;    @Column(name = "title")    private String title;    @Column(name = "content")    private String content;    @Column(name = "created")    private Date created;    @Column(name = "modified")    private Date modified;    @Column(name = "categories")    private String categories;    @Column(name = "tags")    private String tags;    @Column(name = "allow_comment")    private Integer allowComment;    @Column(name = "thumbnail")    private String thumbnail;}

ArticleDao层实现

public interface ArticleDao extends JpaRepository {}

Article Service 层

ArticleService接口

public interface ArticleService {    Page
getArticleWithPage(Integer page, Integer size);}

ArticleServiceImpl实现类

@Service@Slf4jpublic class ArticleServiceImpl implements ArticleService {    @Autowired    private ArticleDao articleDao;    @Override    public Page
getArticleWithPage(Integer page, Integer size) { log.info("page is {}, size is {}", page, size); if (page <= 0) { page = 1; } Pageable pageRequest = PageRequest.of(page - 1, size); return articleDao.findAll(pageRequest); }}

ArticleController 控制层实现

@Controller@Slf4j@RequestMapping("/article")public class ArticleController {    @Autowired    private ArticleService articleService;    @RequestMapping("/index")    public String toIndex(Model model,                          @RequestParam(value = "page", defaultValue = "1") Integer page,                          @RequestParam(value = "size", defaultValue = "3") Integer size) {        Page
articles = articleService.getArticleWithPage(page, size); model.addAttribute("articles", articles); return "/client/index"; }}

页面核心代码

子慕

about me

Java后台开发

个人博客小站,主要发表关于Java、Spring、Docker等相关文章

联系我

效果展示

以上就是springboot中怎么利用Jpa 实现分页功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

0