以前咱们学习了如何使用Jpa访问关系型数据库。比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门。java
经过Jpa大大简化了咱们对数据库的开发工做。可是,以前的例子中咱们只提到了最简单的CRUD(增删改查)操做。实际上,Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,咱们来学习如何经过Pageable
来对数据库进行分页查询。web
首先咱们须要引入Jpa,数据库直接使用hsqldb
内存数据库就能够了:spring
<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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> </parent> <groupId>tmy</groupId> <artifactId>demo.jpa.page</artifactId> <version>0.0.1-SNAPSHOT</version> <name>tmy-spring-jpa-page-demo</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
Jpa的基本使用方法在如何使用Jpa访问关系型数据库已经介绍过,咱们暂且跳过,这里咱们直接来看接口BlogRepository
的定义:sql
public interface BlogRepository extends PagingAndSortingRepository<Blog, Integer> { Page<Blog> findByDeletedFalse(Pageable pageable); }
咱们能够看到,BlogRepository
定义了这样一个方法:Page<Blog> findByDeletedFalse(Pageable pageable);
,咱们主要关注它的参数以及返回值。数据库
pageNumber
、pageSize
等),这样,Jpa就可以经过pageable参数来获得一个带分页信息的Sql语句。Spring Data Jpa除了会经过命名规范帮助咱们扩展Sql语句外,还会帮助咱们处理类型为Pageable
的参数,将pageable参数转换成为sql'语句中的条件,同时,还会帮助咱们处理类型为Page
的返回值,当发现返回值类型为Page
,Spring Data Jpa将会把数据的总体信息、当前数据的信息,分页的信息都放入到返回值中。这样,咱们就可以方便的进行个性化的分页查询。apache
Pageable
只是一个抽象的接口,那么,家下来咱们学习如何得到pageable对象。json
Pageable
定义了不少方法,但其核心的信息只有两个:一是分页的信息(page、size),二是排序的信息。Spring Data Jpa提供了PageRequest
的具体实现,Spring MVC提供了对Spring Data JPA很是好的支持,咱们只提供分页以及排序信息便可:spring-mvc
@RequestMapping(value = "/params", method=RequestMethod.GET) public Page<Blog> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) { Sort sort = new Sort(Direction.DESC, "id"); Pageable pageable = new PageRequest(page, size, sort); return blogRepository.findAll(pageable); }
在这里,咱们经过参数得到分页的信息,并经过Sort
以及Direction
告诉pageable须要经过id逆序排列。Spring MVC的介绍情移步这里:Spring MVC快速入门。mvc
这里能够看到,经过参数来获得一个pageable对象仍是比较繁琐的,当查询的方法比较多的时候,会产生大量的重复代码。为了不这种状况,Spring Data提供了直接生成pageable的方式。app
@RequestMapping(value = "", method=RequestMethod.GET) public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) { return blogRepository.findAll(pageable); }
咱们能够看到,咱们只须要在方法的参数中直接定义一个pageable类型的参数,当Spring发现这个参数时,Spring会自动的根据request的参数来组装该pageable对象,Spring支持的request参数以下:
property,property(,ASC|DESC)
的方式组织,例如sort=firstname&sort=lastname,desc
表示在按firstname正序排列基础上按lastname倒序排列这样,咱们就能够经过url的参数来进行多样化、个性化的查询,而不须要为每一种状况来写不一样的方法了。
经过url来定制pageable很方便,但惟一的缺点是不太美观,所以咱们须要为pageable设置一个默认配置,这样不少状况下咱们都可以经过一个简洁的url来获取信息了。
Spring提供了@PageableDefault
帮助咱们个性化的设置pageable的默认配置。例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
表示默认状况下咱们按照id倒序排列,每一页的大小为15。
最后,让咱们进入程序的根目录,运行命令mvn spring-boot:run
将web应用启动起来,启动完成后,访问http://localhost:8080/
页面,咱们将看到以下结果:
{
"content":[ {"id":123,"title":"blog122","content":"this is blog content"}, {"id":122,"title":"blog121","content":"this is blog content"}, {"id":121,"title":"blog120","content":"this is blog content"}, {"id":120,"title":"blog119","content":"this is blog content"}, {"id":119,"title":"blog118","content":"this is blog content"}, {"id":118,"title":"blog117","content":"this is blog content"}, {"id":117,"title":"blog116","content":"this is blog content"}, {"id":116,"title":"blog115","content":"this is blog content"}, {"id":115,"title":"blog114","content":"this is blog content"}, {"id":114,"title":"blog113","content":"this is blog content"}, {"id":113,"title":"blog112","content":"this is blog content"}, {"id":112,"title":"blog111","content":"this is blog content"}, {"id":111,"title":"blog110","content":"this is blog content"}, {"id":110,"title":"blog109","content":"this is blog content"}, {"id":109,"title":"blog108","content":"this is blog content"}], "last":false, "totalPages":9, "totalElements":123, "size":15, "number":0, "first":true, "sort":[{ "direction":"DESC", "property":"id", "ignoreCase":false, "nullHandling":"NATIVE", "ascending":false }], "numberOfElements":15 }
经过查询结果,咱们能够知道:
怎么样,信息是否是很丰富,代码是否是很简单,快点来尝试一下Jpa的分页查询吧。