//工具类,增删改查等等
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先排序再分页。的更多相关文章

  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. [转]字符编码笔记:ASCII,Unicode 和 UTF-8

      本文非原创,转载 ,原文地址 :http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 作者: 阮一峰 日期: 20 ...

  2. rabbimq之死信队列

    死信队列:DLX,dead-letter-exchange 利用dlx,当消息在一个队列中变成死信(dead message)之后,它能被重新publish到另一个exchange,这个exchang ...

  3. php 正则验证

      PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判 ...

  4. hibernate下Session的获取方式

    Session是应用程序与数据库之间的一个会话,其重要性不言而喻.初学Hibernate,使用SessionFactory,老老实实地打开事务,提交,回滚,关闭session. 1.直接通过Sessi ...

  5. 第4章 css文字text与字体font-face

    text-overflow 与 word-wrap text-overflow:用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出 ...

  6. javascript 文字大小自动适应文本框 (文字大小自动调整)

    javascript 文字大小自动适应文本框 (文字大小自动调整) TOC 思考 思考一:面积法 思考二:微调法 代码 在进行类似微博墙之类的展示页面中,经常会遇到这样的需求:在固定大小的区域放入字数 ...

  7. mui ajax 应用的跨域问题

    1.首先在mui.ajax的error函数里出现: “syntaxerror unexpected token <” 这样的错误,那么在 mui.ajax中的type写成  JSONP ,后台需 ...

  8. C#虚基类继承与接口的区别

    类:定义新的数据类型以及这些新的数据类型进行相互操作的方法 定义方式: class Cat { } class Cat:object { } C#中所有的类都是默认由object类派生来的,显示指定或 ...

  9. RC4 加解密

    加密解密都是这一个接口 int Rc4EncryptFile(std::string in_file_path, std::string out_file_path, const char *rc4_ ...

  10. scanf和scanf_s在VS2013中的使用

    转载:https://www.cnblogs.com/liuchaojiayou/p/4418215.html 在VS2013中,每次使用scanf都会报错:This function or vari ...