数据库内容如下:
使用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,这样就是后端分页了。将其返回值封装下,就可以给前端了。