Spring Boot笔记-JPA分页(后端分页)

140 篇文章 4 订阅
47 篇文章 1 订阅
本文展示了如何在Spring Boot项目中使用JPA和Pageable实现分页查询。通过配置Maven依赖,创建ORM对象,定义Repository接口以及Service,详细阐述了如何获取并返回分页数据,包括总记录数和当前页信息。
摘要由CSDN通过智能技术生成

数据库内容如下:

使用Pageable即可。

Maven如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JPAPageable</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>

ORM的对象是这样的TestDemo.java:

package cn.it1995.object;

import javax.persistence.*;

@Entity
@Table(name = "test_demo", schema = "TESTDB", catalog = "")
public class TestDemo {
    private long id;
    private String value1;
    private Integer value2;

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "value_1")
    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

    @Basic
    @Column(name = "value_2")
    public Integer getValue2() {
        return value2;
    }

    public void setValue2(Integer value2) {
        this.value2 = value2;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TestDemo testDemo = (TestDemo) o;

        if (id != testDemo.id) return false;
        if (value1 != null ? !value1.equals(testDemo.value1) : testDemo.value1 != null) return false;
        if (value2 != null ? !value2.equals(testDemo.value2) : testDemo.value2 != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (value1 != null ? value1.hashCode() : 0);
        result = 31 * result + (value2 != null ? value2.hashCode() : 0);
        return result;
    }
}

仓库如下:

TestDemoRepository.java

package cn.it1995.repository;

import cn.it1995.object.TestDemo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TestDemoRepository extends JpaRepository<TestDemo, Long> {
}

service端如下:

TestDemoService.java

package cn.it1995.service;

import cn.it1995.object.TestDemo;
import cn.it1995.repository.TestDemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class TestDemoService {

    @Autowired
    private TestDemoRepository testDemoRepository;

    public Page<TestDemo> listAll(Integer pageNum){

        Pageable pageable = PageRequest.of(pageNum - 1, 10);
        Page<TestDemo> all = testDemoRepository.findAll(pageable);
        return all;
    }
}

关键的就是这个,分页我们需要的,一共多少条数据,当前是第几页:

这里可以看到,当前page为0,当前总数是37,这样就是后端分页了。将其返回值封装下,就可以给前端了。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值