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 ...
随机推荐
- Oracle数据库查看已添加的索引和创建索引
/** *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'tablename%'; --查 ...
- 二:SpringAOP
一:AOP 面向切面编程思想 横向重复,纵向抽取 |- filter中 |- 动态代理 |- interceptor中 二:动态代理 1.通过动态代理可以体现aop思想 2.对目标对象中的方法进行增强 ...
- ps命令详解加例子
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- shell编程之export
shell 与 export命令用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令 或声明变量,也可以创建并运行shell脚本程序.运行shell脚本 ...
- unity3d之控制人物转向移动并播放动画
tip:transition 勾选Has Exit Time B动画播放完毕后就可以自己返回A不用代码控制.因为想做一个小人静止时 隔两秒会摆动小手的特效. 附上代码参考: using UnityEn ...
- Object.defineProperty使用技巧
Object.definedProperty 该方法允许精确添加或修改对象的属性.通过赋值操作添加的普通属性是可枚举的,能够在属性枚举期间呈现出来(for...in 或 Object.keys 方法) ...
- css-css的基本选择器(三种)
** 要对哪个标签里面的数据进行操作 (1)标签选择器 div { background-color:red; color:blue; } (2)class选择器 * 每个HTML标签中都有一个属性 ...
- 01_java虚拟机基础入门
[Java虚拟机的基本结构] [ 1.类加载子系统 ] 负责从文件系统或者网络中加载Class信息,加载的信息存放在一块称之为方法区的内存空间. [ 2.方法区 ] 存放类信息.常量信息.常量池信息, ...
- json字串转换成泛型类
webrequst发送到指定的url using System; using System.Collections.Generic; using Newtonsoft.Json; using Syst ...
- 任务十七:零基础JavaScript编码(五)
任务目的 在上一任务基础上继续JavaScript的体验 接触更加复杂的表单对象 实现页面上的一个完整交互功能 用DOM实现一个柱状图图表 任务描述 参考以下示例代码,原始数据包含几个城市的空气质量指 ...