spring boot jpa 复杂查询 动态查询 连接and和or 模糊查询 分页查询
最近项目中用到了jpa,刚接触的时候有些激动,以前的到层忽然不用写sql不用去自己实现了,只是取个方法名就实现了,太惊艳了,惊为天人,但是慢慢的就发现不是这么回事了,在动态查询的时候,不知道怎么操作了,忽然感觉这个框架好笨重,在完成很多基础的操作提高效率的同时,
过度的封装也使他用起来不够灵活,整理了下动态查询以及and和or的查询,再次记录下,利己利人。 dao层只要定义一下findAll(Specification specification,PageRequest pageRequest);就可以用了,实现了动态查询,and 和or联合查询,分页查询,以及模糊查询,基本的需求都可以满足了,但是如果在复杂些,需要跨表甚至跨库的话,还是考虑下jdbc把,
不仅效率高也更灵活,加油!
@Override
public SispPage<AnnouncementEntity> findAnnouncementList(Integer pageNo, Integer pageSize, String t1, String t2,
String t3, String t4) {
Sort sort = new Sort(Sort.Direction.DESC, "createTime");
if (pageNo <= 1) {
pageNo = 0;
} else {
pageNo -= 1;
}
PageRequest pageRequest = PageRequest.of(pageNo, pageSize, sort);
Specification<A> specification = new Specification<A>() {
@Override
public Predicate toPredicate(Root<A> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder
criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
List<Predicate> predicateOr = new ArrayList<>();
Predicate conditionPre;
predicates.add(criteriaBuilder.equal(root.get("成员名称").as(String.class), 1));
if (StringUtils.isNotBlank(t1)) {
predicates.add(criteriaBuilder.like(root.get("成员名称").as(String.class), "%" + t1+ "%"));
} if (StringUtils.isNotBlank(t2)) {
predicates.add(criteriaBuilder.equal(root.get("成员名称").as(String.class), t2));
}
if (StringUtils.isNotBlank(t3)) {
predicates.add(criteriaBuilder.equal(root.get("成员名称").as(String.class), t3));
}
if (StringUtils.isNotBlank(t4)) {
predicateOr.add(criteriaBuilder.like(root.get("成员名称").as(String.class), "%" + t4+ "%"));
predicateOr.add(criteriaBuilder.like(root.get("成员名称").as(String.class), "%" + t4+ "%"));
conditionPre = criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new
Predicate[predicates.size()])),
criteriaBuilder.or(predicateOr.toArray(new
Predicate[predicateOr.size()]))).getRestriction();
} else {
conditionPre = criteriaBuilder.and(predicates.toArray(new
Predicate[predicates.size()]));
}
return conditionPre;
}
}; Page<C> c= announcementDao.findAll(specification, pageRequest);
List<C> cList= announcementPage.getContent(); return cList;
}
spring boot jpa 复杂查询 动态查询 连接and和or 模糊查询 分页查询的更多相关文章
- Spring Data JPA中的动态查询 时间日期
功能:Spring Data JPA中的动态查询 实现日期查询 页面对应的dto类private String modifiedDate; //实体类 @LastModifiedDate protec ...
- Spring Boot JPA的查询语句
文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...
- Spring Data JPA 的 Specifications动态查询
主要的结构: 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询. ...
- Spring Boot(五):Spring Boot Jpa 的使用
在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...
- Spring Boot Jpa 的使用
Spring Boot Jpa 介绍 首先了解 Jpa 是什么? Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供了一种 ...
- (转)Spring Boot(五):Spring Boot Jpa 的使用
http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...
- Spring Boot Jpa 表名小写转大写
今天在使用SpringBoot整合Hibernate后创建表,表名为小写,而在linux下,mysql的表名是区分大小写的,因此在我的数据表中,就出现了两个一样的表 act_id_user 和 AC ...
- Spring Boot JPA 中transaction的使用
文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...
- Spring Boot JPA 连接数据库
本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
随机推荐
- WLC5520分布式端口(数据口)使用1G模块!
思科WLC5520有两个分布式端口. 在 10G 模式和 1G 模式之间切换•如果在端口 1 上未安装任何内容,则默认情况下主板配置为 10G 模式.因此,要切换到 1G 模式,则必须在端口 1上安 ...
- CentOS 7下用firewall-cmd控制端口与端口转发
# 将80端口的流量转发至192.168.0.1的8080端口 1.firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toad ...
- [IOI2005]河流
Description Luogu3354 Solution 一道树形dp的题. 首先考虑转移,很简单,就是这个点做不做伐木场.为了方便转移,我们定义状态为\(f_{i,j,k}\)表示点\(i\)及 ...
- Docker on startup: “No activity detected on VM, aborting”
windows下安装的docker,切换到linux,一直处于重启中,最后报No activity detected on VM, aborting错误 上网百度一下这个错误,基本上没人遇到过,最后在 ...
- idea中运行ssm 或springboot项目时,project Structure的配置
ctrl+alt+shift+s进入 project Structure 首先是project选项 Modules 标明source testsource 以及 resource 和 testreso ...
- codeforces 1204C Anna, Svyatoslav and Maps(floyd+dp)
题目链接:http://codeforces.com/problemset/problem/1204/C 给定一组序列,P1,P2,P3...Pm,这是一组合法路径的序列,即任意的Pi和Pi+1之间有 ...
- RegExp-dotAll
//.不能匹配四个字节的utf16字符和行终止符\n,\r console.log(/foo.bar/.test('foo\nbar')) //false //dotAll console.log(/ ...
- fiddler中文乱码解决方案
只用添加一个注册表变量就行 cmd窗口执行regedit命令,在弹出的注册表编辑界面找到fiddler 右击新建一个字符传值 HeaderEncodingGBK 结果如上图右所示~ 重启fiddler ...
- 对象析构谈—— delete this 的使用及注意事项
this对象是必须是用 new操作符分配的(而不是用new[],也不是用placement new,也不是局部对象,也不是global对象): delete this后,不能访问该对象任何的成员变量及 ...
- AcWing 850. Dijkstra求最短路 II 堆优化版 优先队列 稀疏图
//稀疏图 点和边差不多 #include <cstring> #include <iostream> #include <algorithm> #include ...