点击上面“天码营”,加入我们,快速成长~
「内容简介」之前我们学习了如何使用Jpa访问关系型数据库,但是之前的例子中我们只提到了最简单的CRUD(增删改查)操作,接下来我们来学习如何通过来对数据库进行分页查询。
之前我们学习了。通过Jpa大大简化了我们对数据库的开发工作。但是,之前的例子中我们只提到了最简单的CRUD(增删改查)操作。实际上, Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学习如何通过来对数据库进行分页查询。
添加maven依赖
首先我们需要引入Jpa,数据库直接使用内存数据库就可以了:
4.0.0 org.springframework.boot spring-boot-starter-parent 1.2.5.RELEASE tmy demo.jpa.page 0.0.1-SNAPSHOT tmy-spring-jpa-page-demo org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.hsqldb hsqldb runtime
继承
Jpa的基本使用方法在已经介绍过,我们暂且跳过,这里我们直接来看接口的定义:
public interface BlogRepository extends PagingAndSortingRepository { Page findByDeletedFalse(Pageable pageable); }
我们可以看到jpa分页,定义了这样一个方法:Page ( );,我们主要关注它的参数以及返回值。
Data Jpa除了会通过命名规范帮助我们扩展Sql语句外,还会帮助我们处理类型为的参数,将参数转换成为sql'语句中的条件,同时,还会帮助我们处理类型为Page的返回值,当发现返回值类型为Page, Data Jpa将会把数据的整体信息、当前数据的信息,分页的信息都放入到返回值中。这样,我们就能够方便的进行个性化的分页查询。
只是一个抽象的接口,那么,家下来我们学习如何获得对象。
通过参数生成对象
定义了很多方法jpa分页,但其核心的信息只有两个:一是分页的信息(page、size),二是排序的信息。 Data Jpa提供了的具体实现,我们只提供分页以及排序信息即可:
@RequestMapping(value = "/params", method=RequestMethod.GET) public Page 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以及告诉需要通过id逆序排列。
这里可以看到,通过参数来得到一个对象还是比较繁琐的,当查询的方法比较多的时候,会产生大量的重复代码。为了避免这种情况, Data提供了直接生成的方式。
直接获取对象
@RequestMapping(value = "", method=RequestMethod.GET) public Page getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) { return blogRepository.findAll(pageable); }
我们可以看到,我们只需要在方法的参数中直接定义一个类型的参数,当发现这个参数时,会自动的根据的参数来组装该对象,支持的参数如下:
这样,我们就可以通过url的参数来进行多样化、个性化的查询,而不需要为每一种情况来写不同的方法了。
通过url来定制很方便,但唯一的缺点是不太美观,因此我们需要为设置一个默认配置,这样很多情况下我们都能够通过一个简洁的url来获取信息了。
提供了@帮助我们个性化的设置的默认配置。例如@(value = 15, sort = { "id" }, = Sort..DESC)表示默认情况下我们按照id倒序排列,每一页的大小为15。
返回结果
最后,让我们进入程序的根目录,运行命令mvn -boot:run将web应用启动起来,启动完成后,访问: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的分页查询吧。