网上有很多关系Springboot data jpa多条件分页排序查询的例子, 但一般都不是很完整, 有分页,无排序,有排序,无多排序等,这里分享一个带多条件,分页及多排序的一个完整实例, 为了大家完整的查看,这里给出所有的信息, 不省略任何部分
定义实体类:package com.yt.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private int id;
@Column(name = "names", unique = false, nullable = true, length = 50)
private String names;
@Column(name = "email", length = 30)
private String email;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
定义Repository接口package com.yt.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.yt.entity.Student;
public interface StudentRepository extends JpaRepository,JpaSpecificationExecutor{
}
//JpaSpecificationExecutor 这个是为了多条件分页排序使用,如果只是简单的增删改查,就不需要
实现Service层接口package com.yt.service;
import java.util.List;
import org.springframework.data.domain.Page;
import com.yt.entity.Student;
public interface StuService {
Page findByCondition(Integer page, Integer size, String names, String sex);
}
实现Service层接口实现类package com.yt.service;
import java.util.ArrayList;
import java.util.List;
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.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.Predicate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.yt.entity.Student;
import com.yt.repository.StudentRepository;
@Service
public class StuServiceImpl implements StuService{
@Autowired
private StudentRepository stuRepository;
@Override
public Page findByCondition(Integer page, Integer size, String names, String sex) {
//多排序
List list=new ArrayList();
list.add( new Order(Direction.DESC, "id"));
list.add( new Order(Direction.ASC, "email"));
Sort sort = Sort.by(list);
//Pageable 封装了分页的参数,当前页,每页显示的条数,注意:它的当前页是从0开始的
Pageable pageable = PageRequest.of(page-1, size, sort);
Specification spec = (root, criteriaQuery, criteriaBuilder) -> {
List predicates = new ArrayList();
if (!StringUtils.isEmpty(names)){
predicates.add(criteriaBuilder.like(root.get("names"), "%" + names + "%"));
}
if (!StringUtils.isEmpty(sex)){
predicates.add(criteriaBuilder.like(root.get("sex"), sex));
}
//将多条件连接在一起
return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
};
return stuRepository.findAll(spec,pageable);
}
}
控制器层调用:@RequestMapping(value="findallby")
public String findByCondition(
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "size", defaultValue = "10") Integer size,
@RequestParam(value = "names", defaultValue = "") String names,
@RequestParam(value = "sex", defaultValue = "") String sex
)
{
Page result = stuSer.findByCondition(page, size, names, sex);
List list = result.getContent(); //所有的数据
Long totals = result.getTotalElements(); //总条数
int pages = result.getTotalPages(); //总页数
System.out.println("共有" + pages + "页, " + totals + "条");
for(Student s:list) {
System.out.println(s.getNames() + " "+ s.getId());
}
return "已经实现了多条件分页排序查询";
}
浏览器访问,控制台如下
Hibernate: select student0_.id as id1_0_, student0_.email as email2_0_, student0_.names as names3_0_, student0_.sex as sex4_0_ from student student0_ where student0_.names like ? order by student0_.id desc limit ?
共有1页, 3条
hibername 48
hibername 47
hibername 46