需要实现的功能:

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

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

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

话不多说,直接上代码。

实体类

 import lombok.Data;

 import javax.persistence.Entity;
import javax.persistence.Id; /**
* Created by 孙义朗 on 2017/11/14 0014.
*/
@Entity
@Data
public class Employee {
@Id
private String id;
private String name;
private String age;
}

接口

   import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; /**
* Created by 孙义朗 on 2017/11/14 0014.
*/
public interface EmployeeRepository extends JpaRepository<Employee, String>{
List<Employee> findAll(Specification<Employee> spc, Pageable pageable);
}

控制器

   import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*; import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List; /**
* Created by 孙义朗 on 2017/11/14 0014.
*/
@RestController
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository; //查询所有Employee
@PostMapping(value = "/getEmployee/{pageNum}/{pageSize}")
public List<Employee> getEmployee(@PathVariable("pageNum") Integer pageNum,
@PathVariable("pageSize") Integer pageSize,
@RequestBody Employee employee) {
Pageable pageable = new PageRequest(pageNum, pageSize);
List<Employee> eList = employeeRepository.findAll(new Specification<Employee>() {
@Override
public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
Path<String> id = root.get("id");
Path<String> name = root.get("name");
Path<String> age = root.get("age");
List<Predicate> predicates = new ArrayList<>();
if (employee.getId() != null && !employee.getId().equals("")) {
predicates.add(cb.like(id.as(String.class), "%" + employee.getId() + "%"));
}
if (employee.getName() != null && !employee.getName().equals("")) {
predicates.add(cb.like(name.as(String.class), "%" + employee.getName() + "%"));
}
if (employee.getAge() != null && !employee.getAge().equals("")) {
predicates.add(cb.like(age.as(String.class), "%" + employee.getAge() + "%"));
}
Predicate[] pre = new Predicate[predicates.size()];
criteriaQuery.where(predicates.toArray(pre));
return cb.and(predicates.toArray(pre));
}
}, pageable); return eList;
} }

postman测试效果

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

springboot jpa 多条件查询(单表)的更多相关文章

  1. springboot jpa 多条件查询(多表)

    前几天写的,贴上来. 实体类. package com.syl.demo.daomain; import lombok.Data; import javax.persistence.*; /** * ...

  2. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...

  3. Entity Framework 使用sql语句分页(查询单表)

    1.查询单表 var pageSize = 2;//条数 var pageIndex = 2;//索引 var sql = @" SELECT D.* FROM ( SELECT ROW_N ...

  4. Spring Boot Jpa 多条件查询+排序+分页

    事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...

  5. jpa多条件查询重写Specification的toPredicate方法(转)

    Spring Data JPA支持JPA2.0的Criteria查询,相应的接口是JpaSpecificationExecutor.Criteria 查询:是一种类型安全和更面向对象的查询 . 这个接 ...

  6. 使用mybatis框架实现带条件查询-单条件

    之前我们写的查询sql都是没有带条件的,现在来实现一个新的需求,根据输入的字符串,模糊查询用户表中的信息 UserMapper.xml UserMapper.java 与jdbc的比较: 编写测试方法 ...

  7. jpa多条件查询

    首先继承JpaSpecificationExecutor<T>接口 需要用到JpaSpecificationExecutor<T>中的Page<T> findAll ...

  8. MYSQL:查询单表中不同邮箱最近一次发送状态

    1.联系方式表-customer_contact: id email 1 123456@qq.com 2 987643@qq.com 3 hahaha@qq.com 2.发送邮件记录表-contact ...

  9. spring data jpa 组合条件查询封装

    /** * 定义一个查询条件容器 * @author lee * * @param <T> */ public class Criteria<T> implements Spe ...

随机推荐

  1. CSS:如何清除a标签之间的默认留白间距

    即使我们使用了类似 *{margin: 0;padding: 0;} 这样的代码重置了浏览器默认样式,也会发现类似<a>标签这种inline-block元素,它们之间也还存在着间距. de ...

  2. WinForm Column cannot be added because its CellType property is null.

    在Winform  DatatGridView 控件中绑定了一个模型的属性,结果在生成窗口时,发生错误,异常信息如下 : Column cannot be added because its Cell ...

  3. 973. K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  4. 【OCP|052】OCP换题库,052最新题库及答案整理-第10题

    10.Which two are true about consistent database backups? A) They can only be taken when a RECOVERY C ...

  5. [ActionSprit 3.0] FMS远程共享

    package { import flash.display.Sprite; import flash.events.NetStatusEvent; import flash.events.SyncE ...

  6. Python2和Python3共存,pip共存

    使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下面提供详细教程一份. 1.下载python3和python2 进入python官网,链接https:/ ...

  7. 数组其他部分及java常见排序

    数据结构的基本概述: 数据结构是讲什么,其实大概就分为两点: 1.数据与数据之间的逻辑关系:集合.一对一.一对多.多对多 2.数据的存储结构: 一对一的:线性表:顺序表(比如:数组).链表.栈(先进后 ...

  8. 吴裕雄 python 机器学习——聚类

    import numpy as np import matplotlib.pyplot as plt from sklearn.datasets.samples_generator import ma ...

  9. 1095. Maximum Swap —— Weekly Challenge

    题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度. public class Solution { /** * @param num: a non-negative in ...

  10. Hangfire项目

    什么是Hangfire Hangfire 是一个开源的.NET任务调度框架,目前1.6+版本已支持.NET Core.个人认为它最大特点在于内置提供集成化的控制台,方便后台查看及监控: 另外,Hang ...