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中,并给部分字段建立索引, ...
随机推荐
- [z]表空间对应文件的AUTOEXTEND ON NEXT指定的值对性能的影响
创建表空间的时候指定的数据文件可以设为自动扩展,以及每次扩展多少容量,如果发现在大数据量插入的时候非常慢,可能的原因是NEXT指定的值太小.下面来模拟一下这个过程:1,创建一个表空间:CREATE T ...
- 10.15 JS日记
1.JS 介绍 js的全称是JavaScript,它是一门前台语言 Java是一门后台语言 ,它们两个之间毫无关系 JavaScript的作者是布兰登,艾奇 前台语言:运行在客户端 后台语言:与数据库 ...
- poj 2777(线段树+lazy思想) 小小粉刷匠
http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...
- 839A Arya and Bran
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Common tasks that you can perform with the Groovy Script test step
https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html Get test case object To obtain ...
- [转]Firefox+Burpsuite抓包配置(可抓取https)
0x00 以前一直用的是火狐的autoproxy代理插件配合burpsuite抓包 但是最近经常碰到开了代理却抓不到包的情况 就换了Chrome的SwitchyOmega插件抓包 但是火狐不能抓包的问 ...
- TP QQ 微信 微博登录
use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...
- 实现SQL express版做自动备份数据库的方法
SQL Server 2005/2008 Express版没有代理组件,不支持维护计划.可以采用下面的办法实现每日备份: 一.在要备份的数据库中创建存储过程. 存储过程名称:sp_BackupData ...
- openssl 连接 https(nginx)
参考源码路径 demos\ssl #include <stdio.h> #include <string.h> #include <stdlib.h> #incl ...