springboot jpa 多条件查询(单表)

需要实现的功能:

多个搜索输入框;全部不填,则查出所有列表;填了条件,就按条件查找;填的条件个数不定。

方法实现的核心:jpa自带的Specification<T>

(目前只需要单表,多表其实差不多)

 

话不多说,直接上代码。

实体类

 1 import lombok.Data;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.Id;
 5 
 6 /**
 7  * Created by 孙义朗 on 2017/11/14 0014.
 8  */
 9 @Entity
10 @Data
11 public class Employee {
12     @Id
13     private String id;
14     private String name;
15     private String age;
16 }

接口

 1  2  3 import org.springframework.data.jpa.domain.Specification;
 4 import org.springframework.data.jpa.repository.JpaRepository;
 5 
 6 import java.util.List;
 7 
 8 /**
 9  * Created by 孙义朗 on 2017/11/14 0014.
10  */
11 public interface EmployeeRepository extends JpaRepository<Employee, String>{
12     List<Employee> findAll(Specification<Employee> spc, Pageable pageable);
13 }

控制器

 1  2  3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.data.domain.PageRequest;
 5 import org.springframework.data.domain.Pageable;
 6 import org.springframework.data.jpa.domain.Specification;
 7 import org.springframework.web.bind.annotation.*;
 8 
 9 import javax.persistence.criteria.*;
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 /**
14  * Created by 孙义朗 on 2017/11/14 0014.
15  */
16 @RestController
17 public class EmployeeController {
18     @Autowired
19     EmployeeRepository employeeRepository;
20 
21     //查询所有Employee
22     @PostMapping(value = "/getEmployee/{pageNum}/{pageSize}")
23     public List<Employee> getEmployee(@PathVariable("pageNum") Integer pageNum,
24                                       @PathVariable("pageSize") Integer pageSize,
25                                       @RequestBody Employee employee) {
26         Pageable pageable = new PageRequest(pageNum, pageSize);
27         List<Employee> eList = employeeRepository.findAll(new Specification<Employee>() {
28             @Override
29             public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
30                 Path<String> id = root.get("id");
31                 Path<String> name = root.get("name");
32                 Path<String> age = root.get("age");
33                 List<Predicate> predicates = new ArrayList<>();
34                 if (employee.getId() != null && !employee.getId().equals("")) {
35                     predicates.add(cb.like(id.as(String.class), "%" + employee.getId() + "%"));
36                 }
37                 if (employee.getName() != null && !employee.getName().equals("")) {
38                     predicates.add(cb.like(name.as(String.class), "%" + employee.getName() + "%"));
39                 }
40                 if (employee.getAge() != null && !employee.getAge().equals("")) {
41                     predicates.add(cb.like(age.as(String.class), "%" + employee.getAge() + "%"));
42                 }
43                 Predicate[] pre = new Predicate[predicates.size()];
44                 criteriaQuery.where(predicates.toArray(pre));
45                 return cb.and(predicates.toArray(pre));
46             }
47         }, pageable);
48 
49         return eList;
50     }
51 
52 }

postman测试效果

 

 

(多表查询http://www.cnblogs.com/arrrrrya/p/7865090.html)

posted on 2017-11-14 10:36  秀川  阅读(3716)  评论(0编辑  收藏  举报

导航