spring boot jpa 多条件组合查询带分页的案例
spring data jpa 是一个封装了hebernate的dao框架,用于单表操作特别的方便,当然也支持多表,只不过要写sql。对于单表操作,jpake可以通过各种api进行搞定,下面是一个对一个表或者对象进行
多字段查询的案例,可以看到jpa的单表查询是多么的强大
1,先看这个对象--表映射所包含的字段
父类
@MappedSuperclass
public abstract class EntityBase implements Serializable {
/** 0-有效;9-删除 */
private Integer status; //0-有效;9-删除
/** 创建时间 */
private Date createTime; //创建时间
/**
* 创建账号
*/
private Long createUser; //创建账号
/**
* 更新时间
*/
private Date updateTime; //更新时间
/**
* 更新账号
*/
private Long updateUser; //更新账号
这个对象
@Entity
@Table(name = "park_record_pay")
public class ParkRecordPayPo extends EntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/** 所属停车场编号 */
private String idPark;
/** 停车场简单名称 */
@Transient
private String parkName;
/** 进场流水 */
private String idInRecord;
/** 收费流水 */
private String idPayRecord;
/** 收费员名称 */
private String tollName;
/** 车牌号 */
private String carNum;
/** 应收金额【单位分】 */
private Long dueFee;
/** 已收金额【单位分】 */
private Long paidFee;
/** 优惠金额【单位分】 */
private Long couponFee;
/** 实收金额【单位分】 */
private Long netFee;
/** 付款方式 */
private String payMode;
/** 停车时长 */
private Long parkDuration;
/** 付费时间 */
private Date payTime;
这是查询的实现过程
这个是查询date字段时所要用到的工具类,要注入dao层
@Autowired
private ParkRecordPayRepository parkRecordPayRepository;
Date dayjia1(Date date){
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
return date;
}
@Override
public ParkRecordPayPoList getParkRecordPayList(String idPark, ParkRecordPayPo parkPayInfoPo, Integer page, Integer pageSize) {
// 把page ,pagesize 封装到pageable对象里面,并且可以按“updateTime”字段进行排序
Pageable pageable = new PageRequest(page,pageSize,new Sort(Sort.Direction.DESC,"updateTime"));
//这个查询要用匿名函数,所以传入的参数要加final,否则传不进去
final ParkRecordPayPo searchParkPayInfoPo = parkPayInfoPo;
Page<ParkRecordPayPo> searchListPage = parkRecordPayRepository.findAll(new Specification<ParkRecordPayPo>() {
@Override
public Predicate toPredicate(Root<ParkRecordPayPo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
if(searchParkPayInfoPo != null){
List<Predicate> list = new ArrayList<Predicate>();
//下面是拼接各个查询条件,有就拼接,没有就跳过,不做查询条件,用list进行拼接确实方便明了
if (!StringUtils.isEmpty(searchParkPayInfoPo.getIdPark())) {
list.add(cb.equal(root.get("idPark").as(String.class), searchParkPayInfoPo.getIdPark()));
}
//这个是个模糊查询
if (!StringUtils.isEmpty(searchParkPayInfoPo.getCarNum())) {
// list.add(cb.equal(root.get("carNum").as(String.class), searchParkPayInfoPo.getCarNum()));
list.add(cb.like(root.get("carNum").as(String.class), "%" + searchParkPayInfoPo.getCarNum().toUpperCase() + "%"));
}
if (!StringUtils.isEmpty(searchParkPayInfoPo.getPayMode())) {
list.add(cb.equal(root.get("payMode").as(Integer.class), searchParkPayInfoPo.getPayMode()));
}
//这个是对类型为date的字段的操作
if (!StringUtils.isEmpty(searchParkPayInfoPo.getPayTime())&&!StringUtils.isEmpty(searchParkPayInfoPo.getTollName())) {
list.add(cb.between(root.<Date>get("payTime"), searchParkPayInfoPo.getPayTime(), dayjia1(searchParkPayInfoPo.getPayTime())));
if(searchParkPayInfoPo.getTollName().equals("null")){
list.add(cb.isNull(root.get("tollName").as(String.class)));
}else{
list.add(cb.equal(root.get("tollName").as(String.class), searchParkPayInfoPo.getTollName()));
}
}
if (!StringUtils.isEmpty(searchParkPayInfoPo.getPayTime())&&StringUtils.isEmpty(searchParkPayInfoPo.getTollName())) {
list.add(cb.greaterThanOrEqualTo(root.<Date>get("payTime"), searchParkPayInfoPo.getPayTime()));
}
Predicate[] p = new Predicate[list.size()];
criteriaQuery.where(cb.and(list.toArray(p)));
}
criteriaQuery.orderBy(cb.desc(root.get("updateTime").as(Date.class)));
return criteriaQuery.getRestriction();
}
}, pageable);
return ParkRecordPayPoList.wrap(searchListPage.getContent(), Integer.valueOf(searchListPage.getTotalElements() + ""));
}
2 这个是在dao层的实现
public interface ParkRecordPayRepository extends PagingAndSortingRepository<ParkRecordPayPo, Long> ,JpaSpecificationExecutor<ParkRecordPayPo> {
//获取时间轴后的数据并分页输出
Page<ParkRecordPayPo> findByIdParkAndUpdateTimeGreaterThan(String idPark, Date updateTime, Pageable pageable); //获取时间轴后的数据(status = ?)并分页输出
Page<ParkRecordPayPo> findByIdParkAndStatusAndUpdateTimeGreaterThan(String idPark, Integer status
, Date updateTime, Pageable pageable); // 分页查询,屏蔽逻辑删除
Page<ParkRecordPayPo> findByIdParkAndUpdateTimeGreaterThanAndStatusNot(String idPark, Date updateTime
, int status, Pageable pageable); // 根据主键获取客户支付停车费记录
ParkRecordPayPo findById(Long id); // 删除 【逻辑删除】
@Modifying
@Query("update ParkRecordPayPo p set p.status = ?1, p.updateTime = ?2 where p.id = ?3")
int updateStatusAndUpdateTime(Integer status, Date updateTime, Long id); List<ParkRecordPayPo> findAll();
//多条件搜索
List<ParkRecordPayPo> findAll(Specification<ParkRecordPayPo> specification);
//带分页获取,我们用到的就是这个
Page<ParkRecordPayPo> findAll(Specification<ParkRecordPayPo> specification,Pageable pageable); @Query("select p from ParkRecordPayPo p where p.idPark = ?1 and p.payMode = ?2 and date(p.payTime) between ?3 and ?4 order by p.payTime desc ")
List<ParkRecordPayPo> findRecordPay(String idPark, String payMode, Date startDate, Date endDate); @Query("select p from ParkRecordPayPo p where p.idPark = ?1 and date(p.payTime) between ?2 and ?3 order by p.payTime desc")
List<ParkRecordPayPo> findRecordPayNoMode(String idPark, Date startDate, Date endDate);
}
3 配置
dao层的配置
@Configuration
@EnableJpaRepositories("com.yun.park.dao.repository")
@EntityScan("com.yun.park.api.admin.entity")
public class DaoConfig {}
总配置
@Configuration
@Import({DaoConfig.class, ServiceConfig.class})
public class ServerConfig {
@Bean
@ConfigurationProperties("park-server")
ParkServerProperties parkServerProperties() {
return new ParkServerProperties();
}
spring boot jpa 多条件组合查询带分页的案例的更多相关文章
- Spring Boot Jpa 多条件查询+排序+分页
事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...
- 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的查询语句
文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...
- Spring Data JPA中的动态查询 时间日期
功能:Spring Data JPA中的动态查询 实现日期查询 页面对应的dto类private String modifiedDate; //实体类 @LastModifiedDate protec ...
- Spring boot JPA 用自定义主键策略 生成自定义主键ID
最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...
- spring boot JPA中实体类常用注解
spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...
- solr的多条件组合查询和solr的范围查询【转】
solr的多条件组合查询和solr的范围查询 版权声明:本文为博主原创文章,供大家参考,但不要抄袭哦! 存在问题:为了减轻数据库的访问压力,往往我们将必要的数据存储到solr中,并给部分字段建立索引, ...
随机推荐
- better-scroll使用总结
参考:https://zhuanlan.zhihu.com/p/27407024 better-scroll使用小结 核心就是这4个 <script> import BScroll fro ...
- c# 2016QQ自动登录程序
程序是抓QQ主程序窗体句柄,通过移位定位到QQ 输入框,虚拟键盘输入后,ALT切换到密码框的方式实现的 附程序: using System;using System.Collections.Gener ...
- 8.Mysql数据类型选择
8.选择合适的数据类型8.1 CHAR与VARCHAR CHAR固定长度的字符类型,char(n) 当输入长度不足n时将用空格补齐,char(n)占用n个字节,CHAR类型输出时会截断尾部的空格,即使 ...
- Spring 监听
Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...
- iOS.Crash.OniOS8.WhenCall[popToRootViewController]
系统iOS 8.x, ARC. CrashCase: 在UIViewController中有一个类型为UIScrollView的实例变量scrollView, 点击UIViewController中的 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- NC 日志文件注册
在实际开发中,例如接口向外系统发送数据,这些数据前台看不到,一般都是记录日志,然后在后台日志文件中查看.但是,用系统原本日志文件来看,有时会记录一些别的模块日志信息.所以,我们可以注册个自己的模块日志 ...
- 如何查看xmtb项目接口
http://api.xmtb.com/?act=jucheng&op=get_show
- Servlet API
Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...
- js使用sessionStorage、cookie保存token
本文是参考别人的博客写的,图片直接用的别人的 1.Token:token是客户端频繁向服务器端请求数据,服务器频繁的去数据库查询用户名和密码进行对比,判断用户名和密码正确与否,并作出相应的提示,在这样 ...