首先继承JpaSpecificationExecutor<T>接口

需要用到JpaSpecificationExecutor<T>中的Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);//多条件分页查询

ITpSamplerInfoService.java

Page<TpSamplerInfo> findByConditionPage(Long samplerCode,String crossName,Pageable pageable);

TpSamplerInfoServiceImpl.java实现类

@Autowired
ITpSamplerInfoDao samplerInfoDao;//继承JpaSpecificationExecutor<T>

@Override
public Page<TpSamplerInfo> findByConditionPage(Long samplerCode,String crossName, Pageable pageable) {
Page<TpSamplerInfo> pageList = samplerInfoDao.findAll(new Specification<TpSamplerInfo>() {
@Override
public Predicate toPredicate(Root<TpSamplerInfo> root, CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
List<Predicate> list = new ArrayList<Predicate>();
if(samplerCode!=null) {
list.add(criteriaBuilder.like(root.get("samplerCode"), "%"+samplerCode+"%"));
}
if(crossName!=null) {
//外键对象的属性,要用join再get
list.add(criteriaBuilder.like(root.join("tpCrossroadInfo").get("crssroadName"), crossName));
}
Predicate[] array = new Predicate[list.size()];
return criteriaBuilder.and(list.toArray(array));
}
},pageable);
return pageList;
}
//例子2,查询时间段内的记录
@Override
public Page<MapPack> findAll(Integer code, String startData, String endData, Pageable pageable) {
Page<MapPack> pageList = getMapPackDao().findAll(new Specification<MapPack>() { @Override
public Predicate toPredicate(Root<MapPack> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (StringUtils.isNotEmpty(startData) && StringUtils.isNotEmpty(endData)) {
try {
list.add(cb.between(root.get("registerTime"),
sdfmat.parse(sdfmat.format(sdf.parse(startData).getTime())),
sdfmat.parse(sdfmat.format(sdf.parse(endData).getTime() + 86400000))));
} catch (ParseException e) {
e.printStackTrace();
}
}
if(code!=-1) {
list.add(cb.equal(root.join("city").get("id").as(String.class), code));
}
Predicate[] array = new Predicate[list.size()];
return cb.and(list.toArray(array));
}
}, pageable);
return pageList;
}

  

  

 controller

@RequestMapping("/getAllSamplerInfo")
public Map<String,Object> findByConditionPage(@RequestParam("pageNumber") int page,@RequestParam("pageSize") int size,
@RequestParam("samplerCode") Long samplerCode,@RequestParam("crossName") String crossName){
Map<String,Object> map = new HashMap<String, Object>();
Pageable pageable = PageRequest.of(page-1, size, Direction.DESC, "id");
Page<TpSamplerInfo> pageList = tpSamplerInfoService.findByConditionPage(samplerCode,crossName, pageable);
//使用值对象Vo包装一下实体类,解决递归问题
List<TpSamplerInfoVo> list = new ArrayList<>();
pageList.forEach(pl->{
TpSamplerInfoVo tpVo = new TpSamplerInfoVo(pl);
list.add(tpVo);
});
map.put("rows", list);
map.put("total", pageList.getTotalElements()); return map; }

注意:在多条件查询时候查询id主键的时候遇到过一个错误

Parameter value [%3%] did not match expected type [java.lang.Long (n/a)]      意思是参数不匹配预期类型,id是Long类型

解决办法:list.add(criteriaBuilder.like(root.get("id").as(String.class), "%"+crossId+"%"));

加上as(String.class)就不会出现类型不匹配的问题

 

jpa多条件查询的更多相关文章

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

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

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

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

  3. springboot jpa 多条件查询(单表)

    需要实现的功能: 多个搜索输入框:全部不填,则查出所有列表:填了条件,就按条件查找:填的条件个数不定. 方法实现的核心:jpa自带的Specification<T> (目前只需要单表,多表 ...

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

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

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

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

  6. spring jpa : 多条件查询

    https://www.cnblogs.com/Donnnnnn/p/6277872.html 方式一: 第一步:EmpAccNumService package com.payease.scford ...

  7. 【JPA】表达条件查询的关键字

    1.通过解析方法名创建查询 框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find.findBy.read.readBy.get.getBy,然后对剩下部分进行解析.并且如果方法的最后一 ...

  8. Spring Data JPA,一种动态条件查询的写法

    我们在使用SpringData JPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实现. 如果是查询条件是动态的,框架也提供了查询接口. Jpa ...

  9. Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)

    推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...

随机推荐

  1. About custom Theme and Style

    For android system, of course you can custom your own style and theme, but you can't break compatibi ...

  2. 模仿jquery的data

    jquery中,有这个方法 var obj = {}; $.data(obj,'name','jake'); console.info($.data(obj,'name')); console.inf ...

  3. ul li各属性设置

    1. ul li删除前面的黑点:style=“list-style:none;” <ul style=“list-style:none;”> <li></li> & ...

  4. 接口调用 读取图片报错 Access to the path '' is denied.解决方案

    调用接口 读取服务器上 图片 报错: Server was unable to process request. ---> Access to the path '图片路径' is denied ...

  5. pv-remjs的快速开始

    pv-remjs 这是一个移动端适配的工具类,采用rem布局的方式 ## 快速开始 在html文件中引入,先查看版本`<script src= "https://unpkg.com/p ...

  6. C语言--清理getchar缓存

    getchar()采用了缓冲区,而getch()才是立即获取,所以要想再用getchar()获取正确的值必须先清空缓冲区,如果是windows操作系统,用fflush(stdin)函数或rewind( ...

  7. Android 高速录像(1)

    package com.kirin.voltage.activity; import java.io.File;import java.io.IOException;import java.util. ...

  8. C语言输入语句scanf与fgets linux下

    1.测试使用scanf的一个例子: #include "stdio.h" #include "string.h" int main() { char name[ ...

  9. Jmeter参数化设置,多用户登录

    一.模拟多用户登录场景 如登录模式如下图所示,登录界面中需要输入:用户名.密码.验证码 用户名以及密码均是固定值,不需要做处理.验证码需要处理一下,可以后台配置成固定值,具体可以找开发咨询. 在此场景 ...

  10. C# 中重载自增自减操作符的具体运算原理 ----从C++程序员的角度看C#自增操作符重载的实质

    看了看C#的运算符重载,发现与C++打不相同.刚刚被C#的自增操作符坑了,现在来分享一下. 先定义一个类 class A { public int i; public A(int I) { i = I ...