Spring Data JPA中的动态查询 时间日期
功能:Spring Data JPA中的动态查询 实现日期查询
页面对应的dto类
private String modifiedDate;
//实体类
@LastModifiedDate
protected Calendar modifiedDate;
1 public Predicate toPredicate(Root<Infolink> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
2 query.distinct(true);
3 List<Predicate> pl = new ArrayList<Predicate>();
4 Join<Infolink, Infosort> join = (Join<Infolink, Infosort>) root
5 .join(root.getModel().getList("infosorts", Infosort.class), JoinType.LEFT);
6 for (Map f : filters) {
7 String field = f.get("field").toString().trim();
8 String value = f.get("value").toString().trim();
9 if (value != null && value.length() > 0) {
10 if ("infosortId".equalsIgnoreCase(field)) {
11 pl.add(cb.equal(join.get("id"), value));
12 }
13 if ("infolinkTitle".equalsIgnoreCase(field)) {
14 pl.add(cb.like(root.<String>get(field), "%" + value + "%"));
15 }
16 if ("keyword".equalsIgnoreCase(field)) {
17 pl.add(cb.like(root.<String>get(field), "%" + value + "%"));
18 }
19 if ("summary".equalsIgnoreCase(field)) {
20 pl.add(cb.like(root.<String>get(field), "%" + value + "%"));
21 }
22 if ("user".equalsIgnoreCase(field)) {
23 pl.add(cb.equal(root.<String>get(field), value));
24 }
25 if ("infolinkType".equalsIgnoreCase(field)) {
26 pl.add(cb.equal(root.<String>get(field), value));
27 }
28 if ("infolinkState".equalsIgnoreCase(field)) {
29 pl.add(cb.equal(root.<String>get(field), value));
30 }
31 if ("id".equalsIgnoreCase(field)) {
32 pl.add(cb.equal(root.<String>get(field), value));
33 }
34 // 日期查询
35 if ("modifiedDate".equalsIgnoreCase(field)) {
36 // 处理时间
37 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
38 Date startDate;
39 Date endDate;
40 try {
41 startDate = format.parse(value);
42 } catch (ParseException e) {
43 startDate = new Date(946656000000L);// 2000 01 01
44 }
45 endDate = startDate;
46 Calendar calendar = Calendar.getInstance();
47 calendar.setTime(endDate);
48 calendar.add(Calendar.DATE, 1);
49 endDate = calendar.getTime();
50 calendar = null;
51 pl.add(cb.between(root.<Date>get(field), startDate, endDate));
52 }
53 }
54 }
55 pl.add(cb.equal(root.<Integer>get("flag"), 1));
56 return cb.and(pl.toArray(new Predicate[0]));
57 }
效果展示:

================================多条件查询=================================================
二 有多个条件,我们就可以创建一个Predicate集合,最后用CriteriaBuilder的and和or方法进行组合,得到最后的Predicate对象。
root参数是我们用来对应实体的信息的。criteriaBuilder可以制作查询信息。
CriteriaBuilder对象里有很多条件方法,比如制定条件:某条数据的创建日期小于今天。
criteriaBuilder.lessThan(root.get("createDate"), today) //该方法返回的对象类型是Predicate。正是toPredicate需要返回的值。
创建一个Predicate集合,最后用CriteriaBuilder的and和or方法进行组合,得到最后的Predicate对象。 //and到一起的话所有条件就是且关系,or就是或关系了。
public List<WeChatGzUserInfoEntity> findByCondition(Date minDate, Date maxDate, String nickname){
List<WeChatGzUserInfoEntity> resultList = null;
Specification querySpecifi = new Specification<WeChatGzUserInfoEntity>() {
@Override
public Predicate toPredicate(Root<WeChatGzUserInfoEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
if(null != minDate){
predicates.add(criteriaBuilder.greaterThan(root.get("subscribeTime"), minDate));
}
if(null != maxDate){
predicates.add(criteriaBuilder.lessThan(root.get("subscribeTime"), maxDate));
}
if(null != nickname){
predicates.add(criteriaBuilder.like(root.get("nickname"), "%"+nickname+"%"));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
resultList = this.weChatGzUserInfoRepository.findAll(querySpecifi);
return resultList;
}
相关链接:
1. Spring Data JPA,一种动态条件查询的写法
www.cnblogs.com/derry9005/p/6282571.html
2. Spring data jpa 复杂动态查询方式总结
https://blog.csdn.net/qq_30054997/article/details/79420141
3. 带有条件的查询后分页和不带条件查询后分页实现
https://blog.csdn.net/lihuapiao/article/details/48782843
Spring Data JPA中的动态查询 时间日期的更多相关文章
- Spring Data JPA 的 Specifications动态查询
主要的结构: 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询. ...
- Spring data jpa 实现简单动态查询的通用Specification方法
本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...
- Spring data JPA中使用Specifications动态构建查询
有时我们在查询某个实体的时候,给定的条件是不固定的,这是我们就需要动态 构建相应的查询语句,在JPA2.0中我们可以通过Criteria接口查询,JPA criteria查询.相比JPQL,其优势是类 ...
- 【hql】spring data jpa中 @Query使用hql查询 问题
spring data jpa中 @Query使用hql查询 问题 使用hql查询, 1.from后面跟的是实体类 不是数据表名 2.字段应该用实体类中的字段 而不是数据表中的属性 实体如下 hql使 ...
- spring data jpa使用原生sql查询
spring data jpa使用原生sql查询 @Repository public interface AjDao extends JpaRepository<Aj,String> { ...
- 如何在Spring Data JPA中引入Querydsl
一.环境说明 基础框架采用Spring Boot.Spring Data JPA.Hibernate.在动态查询中,有一种方式是采用Querydsl的方式. 二.具体配置 1.在pom.xml中,引入 ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- spring data jpa 利用@Query进行查询
参照https://blog.csdn.net/yingxiake/article/details/51016234#reply https://blog.csdn.net/choushi300/ar ...
随机推荐
- 【转】 详解C中volatile关键字
转自: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有可能 ...
- request.POST 和 request.GET
通过request.POST属性 得到<提交的表单数据>,也是一个类字典对象.request.GET属性 ,得到<URL中的keyvalue请求参数>,也是一个类字典对象.
- WCF 一步一步 发布 WCF服务 到 IIS (图)
WCF 一步一步 发布 WCF服务 到 IIS (图) 使用VS自带的WCFSVCHost(WCF服务主机)发布WCF服务,时刻开发人员测试使用. 下面我们来看一下如何在IIS中部发布一个WCF服务. ...
- Lab 6-1
LABS The goal of the labs for this chapter is to help you to understand the overall functionality of ...
- 胜利大逃亡 HDU - 1253
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0 ...
- 【JS】【1】JavaScript屏蔽Backspace键(避免点击后页面产生回退)
前言: 1,参考资料:JavaScript屏蔽Backspace键 - 孤傲苍狼 - 博客园(http://www.cnblogs.com/xdp-gacl/p/3785806.html) 2,参考的 ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- Oracle数据库,数字强制显示2位小数
在银行.财务等对数字要求敏感的系统中,数字的显示一般有着严格的要求.今遇到一个需求,如题,要求将数字以两位小数的格式显示,如果没有小数,则强制显示为0.例如:123.4 显示为 123.4012 ...
- Django框架(一)
一.web应用 web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C ...
- java method.isBridge
作者:木女孩链接:https://www.zhihu.com/question/54895701/answer/141623158来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...