1. //工具类,增删改查等等
    package com.yunqing.service.impl;
  2.  
  3. import java.util.Map;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.data.domain.Sort;
  10. import org.springframework.data.jpa.domain.Specification;
  11. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  12. import org.springframework.data.repository.PagingAndSortingRepository;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springside.modules.persistence.DynamicSpecifications;
  15. import org.springside.modules.persistence.SearchFilter;
  16.  
  17. public class RootServiceImpl<T> {
  18.  
  19. @Autowired
  20. PagingAndSortingRepository<T, String> pasr;
  21.  
  22. @Autowired
  23. JpaSpecificationExecutor<T> jpas;
  24.  
  25. /**
  26. * 根据条件查询
  27. * @param paramMap
  28. * @param page
  29. * @param rows
  30. * @param clazz
  31. * @return
  32. */
  33. @Transactional(readOnly=true)
  34. public Page<T> findAllByParam(Map<String, Object> paramMap, int page,int rows,Class<T> clazz) {
  35. Map<String, SearchFilter> filters = SearchFilter.parse(paramMap);
  36. Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(),clazz);
  37. Pageable pageable = new PageRequest(page, rows);
  38. Page<T> resultPage = jpas.findAll(spec,pageable);
  39. return resultPage;
  40. }
  41. /**
  42. * 根据ID查询
  43. * @param id
  44. * @return
  45. */
  46. //@Transactional(readOnly=true)
  47. public T findEntityById(String id){
  48. T t=pasr.findOne(id);
  49. return t;
  50. }
  51. /**
  52. * 查询全部
  53. * @return
  54. */
  55. @Transactional(readOnly=true)
  56. public Iterable<T> findAllEntity(){
  57. Iterable<T> list=pasr.findAll();
  58. return list;
  59. }
  60.  
  61. /**
  62. * 删除
  63. * @param id
  64. */
  65. @Transactional
  66. public void deleteEntityById(String id){
  67. pasr.delete(id);
  68. }
  69. /**
  70. * 删除
  71. * @param t
  72. */
  73. @Transactional
  74. public void deleteEntity(T t){
  75. pasr.delete(t);
  76. }
  77. /**
  78. * 删除多个
  79. * @param ids
  80. */
  81. @Transactional
  82. public void deleteEntity(Iterable<T> iterable){
  83. pasr.delete(iterable);
  84. }
  85.  
  86. /**
  87. * 保存
  88. * @param t
  89. */
  90. @Transactional
  91. public void save(T t){
  92. pasr.save(t);
  93. }
  94. /**
  95. * 保存
  96. * @param tList
  97. */
  98. @Transactional
  99. public void save(Iterable<T> tList){
  100. pasr.save(tList);
  101. }
  102. /**
  103. * 删除多个/单个
  104. * <b>deleteEntity</b>
  105. * <p><b>详细说明:</b></p>
  106. *
  107. *@param ids
  108. */
  109. @Transactional
  110. public void deleteEntity(String ids){
  111. if(ids!=null&&!"".equals(ids)){
  112. if(ids.contains(":")){
  113. String[] idArr = ids.split(":");
  114. for(String id : idArr){
  115. deleteEntityById(id);
  116. }
  117. }else{
  118. deleteEntityById(ids);
  119. }
  120. }
  121. }
  122. @Transactional
  123. public Page<T> findAllByParam(Map<String, Object> paramMap, int page,int rows,Class<T> clazz,Sort sort) {
  124. Map<String, SearchFilter> filters = SearchFilter.parse(paramMap);
  125. Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(),clazz);
  126. Pageable pageable = new PageRequest(page, rows,sort);
  127. Page<T> resultPage = jpas.findAll(spec,pageable);
  128. return resultPage;
  129. }
  130. }

service层

  1. package com.yunqing.service.impl;
  2.  
  3. import java.util.Map;
  4.  
  5. import javax.transaction.Transactional;
  6.  
  7. import org.springframework.data.domain.Page;
  8. import org.springframework.data.domain.Sort;
  9. import org.springframework.stereotype.Service;
  10. import com.tideway.live.vo.Student;
  11. import com.tideway.utils.FileUtil;
  12.  
  13. @Service("/studentService")
  14. public class StudentServiceImpl extends RootServiceImpl<Student>{
  15. //查询所有的学生,先排序后查询!!!!!!!!!!!!sort是排序条件参数,在controller层给参数赋值
  16. public Page<Student> queryStudentList(Map<String, Object> paramMap, int page, int rows,Sort sort) {
  17. // TODO Auto-generated method stub
  18. //Page<Student> esultPage = findAllByParam(paramMap, page, rows,Student.class);
  19. Page<Student> esultPage=findAllByParam(paramMap, page, rows, Student.class,sort);
  20. return esultPage;
  21. }
    }

controller层

  1. package com.yunqing.controller;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLDecoder;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import org.apache.commons.collections4.map.HashedMap;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.data.domain.Sort;
  19. import org.springframework.data.domain.Sort.Direction;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.web.bind.WebDataBinder;
  22. import org.springframework.web.bind.annotation.InitBinder;
  23. import org.springframework.web.bind.annotation.ModelAttribute;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.multipart.MultipartFile;
  27.  
  28. import com.tideway.live.service.impl.StudentServiceImpl;
  29. import com.tideway.live.vo.Student;
  30. import com.tideway.utils.FileUtil;
  31.  
  32. @Controller
  33. @RequestMapping("/student")
  34. public class StudentController extends BaseController{
  35.  
  36. @InitBinder("student")
  37. public void initBinder(WebDataBinder binder) {
  38. binder.setFieldDefaultPrefix("student.");
  39. }
  40.  
  41. @Autowired
  42. private StudentServiceImpl studentService;
  43.  
  44. /**
  45. * 获取学员列表
  46. * @param request
  47. * @param response
  48. * @param student
  49. */
  50. @RequestMapping("/queryStudentList")
  51. public void queryStudentList(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("student") Student student){
  52. Map<String, Object> paramMap = new HashedMap<String, Object>();
  53. if(student.getName()!=null&&!"".equals(student.getName())){
  54. paramMap.put("LIKE_name", student.getName());
  55. }
  56. //直接创建sort对象,通过排序方法和属性名
  57. Sort sort=new Sort(Direction.DESC,"createTime");//createTime是学生的录入时间,这样展示的时候就会按录入时间先排序再分页展示。
  58. //Page<Student> page = studentService.queryStudentList(paramMap,this.getPage(request), this.getRows(request));
  59. Page<Student> page = studentService.queryStudentList(paramMap, this.getPage(request), this.getRows(request),sort);
  60. List<Student> list = page.getContent();
  61. Map<String, Object> json = new HashMap<String, Object>();
  62. json.put("total",page.getTotalElements());
  63. json.put("rows",list);
  64. returnJson(response,json);
  65. }
    }

也可以在service层搞定,还可以实现先多条件排序在分页。

  1. public Page<SpecialguardInfo> findAllByParam(Map<String, Object> paramMap, int page, int rows) {
  2. // 先根据状态倒序排列,再根据创建时间倒序排序,再分页。
  3. List<Order> orders=new ArrayList<Sort.Order>();
  4. orders.add(new Order(Direction.DESC, "_state"));
  5. orders.add(new Order(Direction.DESC, "createtime"));
  6. //Sort sort = new Sort(Sort.Direction.DESC,"createtime");
  7. Page<SpecialguardInfo> page1=findAllByParam(paramMap, page, rows, SpecialguardInfo.class, new Sort(orders));//注意参数要修改成这样
  8. return page1;
  9. }

更多精彩文章欢迎关注公众号“Java之康庄大道”

Spring data JPA先排序再分页。的更多相关文章

  1. 【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询

    Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application ...

  2. spring data jpa Specification 复杂查询+分页查询

    当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entitie ...

  3. spring data jpa实现多条件查询(分页和不分页)

    目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...

  4. 使用Spring Data JPA进行数据分页与排序

    一.导读 如果一次性加载成千上万的列表数据,在网页上显示将十分的耗时,用户体验不好.所以处理较大数据查询结果展现的时候,分页查询是必不可少的.分页查询必然伴随着一定的排序规则,否则分页数据的状态很难控 ...

  5. 整合Spring Data JPA与Spring MVC: 分页和排序

    之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...

  6. 整合Spring Data JPA与Spring MVC: 分页和排序pageable

    https://www.tianmaying.com/tutorial/spring-jpa-page-sort Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学 ...

  7. spring data jpa 分页查询

    https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询   法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...

  8. Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)

    推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...

  9. Spring Data Jpa:分页、Specification、Criteria

    分页的主要接口与类 PagingAndSortingRepository 继承自 CrudRepository 接口,提供了排序以及分页查询能力,提供了两个方法 Iterable<T> f ...

随机推荐

  1. Oracle数据库查看已添加的索引和创建索引

    /** *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'tablename%'; --查 ...

  2. 二:SpringAOP

    一:AOP 面向切面编程思想 横向重复,纵向抽取 |- filter中 |- 动态代理 |- interceptor中 二:动态代理 1.通过动态代理可以体现aop思想 2.对目标对象中的方法进行增强 ...

  3. ps命令详解加例子

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  4. shell编程之export

    shell 与 export命令用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令 或声明变量,也可以创建并运行shell脚本程序.运行shell脚本 ...

  5. unity3d之控制人物转向移动并播放动画

    tip:transition 勾选Has Exit Time B动画播放完毕后就可以自己返回A不用代码控制.因为想做一个小人静止时 隔两秒会摆动小手的特效. 附上代码参考: using UnityEn ...

  6. Object.defineProperty使用技巧

    Object.definedProperty 该方法允许精确添加或修改对象的属性.通过赋值操作添加的普通属性是可枚举的,能够在属性枚举期间呈现出来(for...in 或 Object.keys 方法) ...

  7. css-css的基本选择器(三种)

    ** 要对哪个标签里面的数据进行操作 (1)标签选择器 div { background-color:red; color:blue; } (2)class选择器 * 每个HTML标签中都有一个属性 ...

  8. 01_java虚拟机基础入门

    [Java虚拟机的基本结构] [ 1.类加载子系统 ] 负责从文件系统或者网络中加载Class信息,加载的信息存放在一块称之为方法区的内存空间. [ 2.方法区 ] 存放类信息.常量信息.常量池信息, ...

  9. json字串转换成泛型类

    webrequst发送到指定的url using System; using System.Collections.Generic; using Newtonsoft.Json; using Syst ...

  10. 任务十七:零基础JavaScript编码(五)

    任务目的 在上一任务基础上继续JavaScript的体验 接触更加复杂的表单对象 实现页面上的一个完整交互功能 用DOM实现一个柱状图图表 任务描述 参考以下示例代码,原始数据包含几个城市的空气质量指 ...