springboot jpa 多条件查询(单表)
需要实现的功能:
多个搜索输入框;全部不填,则查出所有列表;填了条件,就按条件查找;填的条件个数不定。
方法实现的核心: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 多条件查询(单表)的更多相关文章
- springboot jpa 多条件查询(多表)
前几天写的,贴上来. 实体类. package com.syl.demo.daomain; import lombok.Data; import javax.persistence.*; /** * ...
- oracle查询单表占用空间的大小
oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...
- Entity Framework 使用sql语句分页(查询单表)
1.查询单表 var pageSize = 2;//条数 var pageIndex = 2;//索引 var sql = @" SELECT D.* FROM ( SELECT ROW_N ...
- Spring Boot Jpa 多条件查询+排序+分页
事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...
- jpa多条件查询重写Specification的toPredicate方法(转)
Spring Data JPA支持JPA2.0的Criteria查询,相应的接口是JpaSpecificationExecutor.Criteria 查询:是一种类型安全和更面向对象的查询 . 这个接 ...
- 使用mybatis框架实现带条件查询-单条件
之前我们写的查询sql都是没有带条件的,现在来实现一个新的需求,根据输入的字符串,模糊查询用户表中的信息 UserMapper.xml UserMapper.java 与jdbc的比较: 编写测试方法 ...
- jpa多条件查询
首先继承JpaSpecificationExecutor<T>接口 需要用到JpaSpecificationExecutor<T>中的Page<T> findAll ...
- MYSQL:查询单表中不同邮箱最近一次发送状态
1.联系方式表-customer_contact: id email 1 123456@qq.com 2 987643@qq.com 3 hahaha@qq.com 2.发送邮件记录表-contact ...
- spring data jpa 组合条件查询封装
/** * 定义一个查询条件容器 * @author lee * * @param <T> */ public class Criteria<T> implements Spe ...
随机推荐
- Windows 2012 R2版本下部署IIS网站
Windows 2012 R2是一个比较稳定的服务器版本,本文分享一篇在Windows 2012 R2版本下搭建IIS项目的操作流程. 1. 安装IIS Web服务器 打开远程桌面->控制面板- ...
- Django_Restframwork_APIVIEW视图_源码分析
Django _VIEW视图_源码分析
- AcWing 153. 双栈排序
https://www.acwing.com/problem/content/155/ #include <cstring> #include <iostream> #incl ...
- 搜索下拉 select美化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css样式之标签的查找
css的组成部分:选择器和声明 css的注释: /*这是注释*/ <!DOCTYPE html> <html lang="zh-CN"> <head& ...
- sql92和sql99
sql1992sql分类 1.笛卡尔积 (表乘表) 2.等值连接 表的连接条件使用“=” 3.非等值连接 表的连接条件使用“>.>=. <.<=.!=.any等” 4.自连接 ...
- 读取Excel表格日期类型数据的时候
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel 2.Excel数据处理: Excel存储日期.时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化 1 ...
- 编写高质量代码:Web前端开发修炼之道(四)
这一节是继上一节高质量的Javascript 7)编程实用技巧 1:弹性 从 一个标签区和内容区的实例(就是点击不同的标签菜单显示不同的内容块)来说明不需要每个tabmenu都设置onclick事件, ...
- Angular material mat-icon 资源参考_Hardware
ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...
- alpha-beta搜索算法
alpha-beta搜索(min-max搜索): 简称mfs,用来解决双方最优决策博弈问题. 核心思想:在搜索树中,下一层越小,对当前层越有利,由于取max,一旦下一层出现了比其他孩子结果更大的值,那 ...