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中,并给部分字段建立索引, ...
随机推荐
- BitCoinCore配置文件解读
bitcoin.conf 配置文件 除了 -datadir 和 -conf 以外的所有命令行参数都可以通过一个配置文件来设置,而所有配置文件中的选项也都可以在命令行中设置.命令行参数设置的值会覆盖配置 ...
- SQL删除重复数据只保留一条数据
1.表结构与数据: CREATE TABLE tablezzl( id int, name ) ); 2.查询出重复的数据: 3.查询出要保留的重复数据: 4.最终的SQL: DELETE FROM ...
- opencv 双边模糊,膨胀腐蚀 开 闭操作
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; int main(int argc, ...
- spring boot (一): Hello World
什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- nim博弈
尼姆博弈 1.问题模型:有三堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 2.解决思路:用(a,b,c)表示某种局势,显证(0,0,0)是第一种奇异 ...
- 找不到或无法加载主类(Could not find or load main class )
在Linux环境下,写了一个简单的java程序,通过javac编译成class文件,然后用java 运行的时候,报了这个错误, 搜了一下,可能是classpath的问题,所以用echo $CLASSP ...
- BZOJ1057或洛谷1169 [ZJOI2007]棋盘制作
BZOJ原题链接 洛谷原题链接 设\(L[i][j],R[i][j],H[i][j]\)表示点\((i,j)\)向左.右.上尽量拓展的左端点.右端点.上端点的坐标. \(L,R\)直接初始化好,\(H ...
- linux以16进制方式查看文件
vim打开文件 :%!xxd 以16进制查看 :%!xxd -r 转回来
- Eigen中的map
Map类用于通过C++中普通的连续指针或者数组 (raw C/C++ arrays)来构造Eigen里的Matrix类,这就好比Eigen里的Matrix类的数据和raw C++array 共享了一片 ...
- TCP与UDP传输协议
目录结构: contents structure [-] 1 TCP协议和UDP协议的比较 1.1 TCP协议 TCP的全称是Transmission Control Protocol (传输控制协议 ...