當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot笔记-JPA分页(后端分页)
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot笔记-JPA分页(后端分页)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據庫內容如下:
使用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;}@Overridepublic 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;}@Overridepublic 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 {@Autowiredprivate 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,這樣就是后端分頁了。將其返回值封裝下,就可以給前端了。
?
?
總結
以上是生活随笔為你收集整理的Spring Boot笔记-JPA分页(后端分页)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java工作笔记-JPA中Reposit
- 下一篇: Python笔记-BeautifulSo