Spring data JPA先排序再分页。
//工具类,增删改查等等
package com.yunqing.service.impl; import java.util.Map; 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.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter; public class RootServiceImpl<T> { @Autowired
PagingAndSortingRepository<T, String> pasr; @Autowired
JpaSpecificationExecutor<T> jpas; /**
* 根据条件查询
* @param paramMap
* @param page
* @param rows
* @param clazz
* @return
*/
@Transactional(readOnly=true)
public Page<T> findAllByParam(Map<String, Object> paramMap, int page,int rows,Class<T> clazz) {
Map<String, SearchFilter> filters = SearchFilter.parse(paramMap);
Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(),clazz);
Pageable pageable = new PageRequest(page, rows);
Page<T> resultPage = jpas.findAll(spec,pageable);
return resultPage;
}
/**
* 根据ID查询
* @param id
* @return
*/
//@Transactional(readOnly=true)
public T findEntityById(String id){
T t=pasr.findOne(id);
return t;
}
/**
* 查询全部
* @return
*/
@Transactional(readOnly=true)
public Iterable<T> findAllEntity(){
Iterable<T> list=pasr.findAll();
return list;
} /**
* 删除
* @param id
*/
@Transactional
public void deleteEntityById(String id){
pasr.delete(id);
}
/**
* 删除
* @param t
*/
@Transactional
public void deleteEntity(T t){
pasr.delete(t);
}
/**
* 删除多个
* @param ids
*/
@Transactional
public void deleteEntity(Iterable<T> iterable){
pasr.delete(iterable);
} /**
* 保存
* @param t
*/
@Transactional
public void save(T t){
pasr.save(t);
}
/**
* 保存
* @param tList
*/
@Transactional
public void save(Iterable<T> tList){
pasr.save(tList);
}
/**
* 删除多个/单个
* <b>deleteEntity</b>
* <p><b>详细说明:</b></p>
*
*@param ids
*/
@Transactional
public void deleteEntity(String ids){
if(ids!=null&&!"".equals(ids)){
if(ids.contains(":")){
String[] idArr = ids.split(":");
for(String id : idArr){
deleteEntityById(id);
}
}else{
deleteEntityById(ids);
}
}
}
@Transactional
public Page<T> findAllByParam(Map<String, Object> paramMap, int page,int rows,Class<T> clazz,Sort sort) {
Map<String, SearchFilter> filters = SearchFilter.parse(paramMap);
Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(),clazz);
Pageable pageable = new PageRequest(page, rows,sort);
Page<T> resultPage = jpas.findAll(spec,pageable);
return resultPage;
}
}
service层
package com.yunqing.service.impl; import java.util.Map; import javax.transaction.Transactional; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.tideway.live.vo.Student;
import com.tideway.utils.FileUtil; @Service("/studentService")
public class StudentServiceImpl extends RootServiceImpl<Student>{
//查询所有的学生,先排序后查询!!!!!!!!!!!!sort是排序条件参数,在controller层给参数赋值
public Page<Student> queryStudentList(Map<String, Object> paramMap, int page, int rows,Sort sort) {
// TODO Auto-generated method stub
//Page<Student> esultPage = findAllByParam(paramMap, page, rows,Student.class);
Page<Student> esultPage=findAllByParam(paramMap, page, rows, Student.class,sort);
return esultPage;
}
}
controller层
package com.yunqing.controller; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import com.tideway.live.service.impl.StudentServiceImpl;
import com.tideway.live.vo.Student;
import com.tideway.utils.FileUtil; @Controller
@RequestMapping("/student")
public class StudentController extends BaseController{ @InitBinder("student")
public void initBinder(WebDataBinder binder) {
binder.setFieldDefaultPrefix("student.");
} @Autowired
private StudentServiceImpl studentService; /**
* 获取学员列表
* @param request
* @param response
* @param student
*/
@RequestMapping("/queryStudentList")
public void queryStudentList(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("student") Student student){
Map<String, Object> paramMap = new HashedMap<String, Object>();
if(student.getName()!=null&&!"".equals(student.getName())){
paramMap.put("LIKE_name", student.getName());
}
//直接创建sort对象,通过排序方法和属性名
Sort sort=new Sort(Direction.DESC,"createTime");//createTime是学生的录入时间,这样展示的时候就会按录入时间先排序再分页展示。
//Page<Student> page = studentService.queryStudentList(paramMap,this.getPage(request), this.getRows(request));
Page<Student> page = studentService.queryStudentList(paramMap, this.getPage(request), this.getRows(request),sort);
List<Student> list = page.getContent();
Map<String, Object> json = new HashMap<String, Object>();
json.put("total",page.getTotalElements());
json.put("rows",list);
returnJson(response,json);
}
}
也可以在service层搞定,还可以实现先多条件排序在分页。
public Page<SpecialguardInfo> findAllByParam(Map<String, Object> paramMap, int page, int rows) {
// 先根据状态倒序排列,再根据创建时间倒序排序,再分页。
List<Order> orders=new ArrayList<Sort.Order>();
orders.add(new Order(Direction.DESC, "_state"));
orders.add(new Order(Direction.DESC, "createtime"));
//Sort sort = new Sort(Sort.Direction.DESC,"createtime");
Page<SpecialguardInfo> page1=findAllByParam(paramMap, page, rows, SpecialguardInfo.class, new Sort(orders));//注意参数要修改成这样
return page1;
}
更多精彩文章欢迎关注公众号“Java之康庄大道”

Spring data JPA先排序再分页。的更多相关文章
- 【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询
Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application ...
- spring data jpa Specification 复杂查询+分页查询
当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entitie ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
- 使用Spring Data JPA进行数据分页与排序
一.导读 如果一次性加载成千上万的列表数据,在网页上显示将十分的耗时,用户体验不好.所以处理较大数据查询结果展现的时候,分页查询是必不可少的.分页查询必然伴随着一定的排序规则,否则分页数据的状态很难控 ...
- 整合Spring Data JPA与Spring MVC: 分页和排序
之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...
- 整合Spring Data JPA与Spring MVC: 分页和排序pageable
https://www.tianmaying.com/tutorial/spring-jpa-page-sort Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学 ...
- spring data jpa 分页查询
https://www.cnblogs.com/hdwang/p/7843405.html spring data jpa 分页查询 法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特 ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- Spring Data Jpa:分页、Specification、Criteria
分页的主要接口与类 PagingAndSortingRepository 继承自 CrudRepository 接口,提供了排序以及分页查询能力,提供了两个方法 Iterable<T> f ...
随机推荐
- Backbone之温故而知新1-MVC
在忙碌了一段时间之后,又有了空余时间来学习新的东西,自从上次研究了backbone之后,一直不得入门,今天有时间有温故了一次,有了些许进步在此记录下, 在开始之前,不得不提一下我的朋友给了我“豆瓣音乐 ...
- C#,动态加载DLL,通过反射,调用参数,方法,窗体
.net中常会用到动态加载DLL,而DLL中可能包含各种参数.方法.窗体,如何来调用动态加载这些参数.方法.窗体呢? 在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系: System. ...
- powerdesigner 将表中name列值复制到comment列 (保留原有comment)
/** * PowerDesigner里面将表中name列值复制到comment列 * @see --------------------------------------------------- ...
- lintcode 刷题记录··
单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...
- js两个字符串明明一样却判断显示不相等
一.问题 两个字符串看起来一样.类型一样,判断str1==str2时返回false: 二.原因 字符串可能含有其他特殊字符:换行符(%D).空格(%20)...一般不显示. 三.如何判断 encode ...
- flask 简易注册登陆
db.py import MySQLdb conn = MySQLdb.connect(', 'test1') cur = conn.cursor() def addUser (username,pa ...
- 初学者需要IPython 与 Jupyter Notebook 吗?
ipython 是 jupyter notebook的前身并拥有ipython的全部功能 jupyter拥有 cell, markdown 整合的功能, 能同时运行代码, 而且是多组的 ...
- linux下通过NFS将远程磁盘mount到本地
最近由于项目原因需要和其他两家公司对接,需要取对方服务器中的图像数据,原本约定是三方都通过http协议来进行通讯,奈何对接方不配合,说文件就在他们服务器放着,怎么取他们不管.所以采取将对方服务器磁盘挂 ...
- 【阿里云产品公测】云引擎ACE公测感受
听说阿里云ACE开始公测了,怀着激动的心情赶紧试用了一下. 这是我用ACE做出来的效果:http://haoyuming.aliapp.com/ 大家点点看看啊 A*W/Q<~I :eSwX ...
- Python学习系列----第三章 控制流
在python中有三种控制流语句,分别是: if.for.while. 2.1 if 语句 if 语句用来检验一个条件,如果条件为真,我们运行一块语句(称为 if-块),否 则我们处理另外一块语句(称 ...