首先来说一下分页和排序所用到的Page、Pageable接口和Sort类都是什么
JpaRepository提供了两个和分页和排序有关的查询
List findAll(Sort sort) 返回所有实体,按照指定顺序排序返回
List findAll(Pageable pageable) 返回实体列表,实体的offest和limit通过pageable来指定
Sort对象用来指示排序,最简单的Sort对象构造可以传入一个属性名列表(不是数据库列名,是属性名),默认采用升序排序。例:
Sort sort = new Sort("id");
//或 Sort sort = new Sort(Direction.ASC,"id");
return userDao.findAll(sort);
程序将查询所有user并按照id进行生序排序。Sort还包括其他一些构造方法,在这里就不一一赘述。
Pageable接口用于构造翻页查询,PageRequest是其实现类,可以通过提供的工厂方法创建PageRequest:
public static PageRequest of(int page, int size)
也可以在PageRequest中加入排序:
public static PageRequest of(int page, int size, Sort sort)
方法中的参数,page总是从0开始,表示查询页,size指每页的期望行数。
Page接口可以获得当前页面的记录、总页数、总记录数、是否有上一页或下一页等。Spring Data翻页查询总是返回Page对象,Page对象提供了以下常用的方法:
int getTotalPages() 总的页数
long getTotalElements() 返回总数
List getContent() 返回此次查询的结果集
代码实现:
1.建立SpringBoot工程,在pom.xml中添加以下依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
runtime
2.创建实体类
package org.gzc.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Marker {
@Id
@GeneratedValue
private int id;
private double lng;
private double lat;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
@Override
public String toString() {
return "Marker [id=" + id + ", lng=" + lng + ", lat=" + lat + "]";
}
}
3.编写dao层接口
package org.gzc.dao;
import org.gzc.entity.Marker;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MarkerDao extends JpaRepository{
}
4.编写service层接口
package org.gzc.service;
import java.util.List;
import org.gzc.entity.Marker;
import org.springframework.data.domain.Pageable;
public interface MarkerService {
void saveMarker(Marker marker);
Page findMarker(Pageable pageable);
}
5.编写service层实现类
package org.gzc.serviceimpl;
import java.util.List;
import org.gzc.dao.MarkerDao;
import org.gzc.entity.Marker;
import org.gzc.service.MarkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class MarkerServiceImpl implements MarkerService{
@Autowired
private MarkerDao markerDao;
@Override
public void saveMarker(Marker marker) {
markerDao.save(marker);
}
@Override
public Page findMarker(Pageable pageable) {
return markerDao.findAll(pageable);
}
}
6.编写controller
package org.gzc.controller;
import org.gzc.entity.Marker;
import org.gzc.service.MarkerService;
import org.gzc.util.Result;
import org.gzc.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MapHandlerController {
@Autowired
private MarkerService markerService;
@SuppressWarnings("rawtypes")
@PostMapping("/saveMarker")
public Result saveMarkerController(@RequestBody Marker marker){
System.out.println(marker);
if (marker!=null) {
markerService.saveMarker(marker);
return ResultUtil.success();
}
return ResultUtil.error(1, "保存失败", "/saveMarker");
}
@SuppressWarnings("rawtypes")
@GetMapping("/showMarkerCount")
public Result returnMarkerCount(){
long count = markerService.markerCount();
System.out.println("count------------------->"+count);
return ResultUtil.success(count, "/showMarkerCount");
}
@SuppressWarnings("rawtypes")
@GetMapping("/showMarkerByPage/{page}")
public Result showMarkerController(@PathVariable("page") int page){
PageRequest pageRequest = PageRequest.of(page, 5);
Page markerPage = markerService.findMarker(pageRequest);
for (int i = 0; i < markerPage.getContent().size(); i++) {
System.out.println(markerPage.getContent().get(i));
System.out.println(markerPage.getTotalElements());
}
if (markerPage.getContent()!=null) {
return ResultUtil.success(markerPage.getContent(), "/showMarker");
}else {
return ResultUtil.error(1, "查询失败", "/showMarker");
}
}
}
后台先给前台传过去数据总量,前台计算完显示第几页,再将第几页传送给后台,后台进行查询并返回数据