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 多条件组合查询带分页的案例的更多相关文章

  1. Spring Boot Jpa 多条件查询+排序+分页

    事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...

  2. Spring Boot(五):Spring Boot Jpa 的使用

    在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...

  3. Spring Boot Jpa 的使用

    Spring Boot Jpa 介绍 首先了解 Jpa 是什么? Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供了一种 ...

  4. (转)Spring Boot(五):Spring Boot Jpa 的使用

    http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...

  5. Spring Boot JPA的查询语句

    文章目录 准备工作 Containing, Contains, IsContaining 和 Like StartsWith EndsWith 大小写不敏感 Not @Query Spring Boo ...

  6. Spring Data JPA中的动态查询 时间日期

    功能:Spring Data JPA中的动态查询 实现日期查询 页面对应的dto类private String modifiedDate; //实体类 @LastModifiedDate protec ...

  7. Spring boot JPA 用自定义主键策略 生成自定义主键ID

    最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...

  8. spring boot JPA中实体类常用注解

    spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...

  9. solr的多条件组合查询和solr的范围查询【转】

    solr的多条件组合查询和solr的范围查询 版权声明:本文为博主原创文章,供大家参考,但不要抄袭哦! 存在问题:为了减轻数据库的访问压力,往往我们将必要的数据存储到solr中,并给部分字段建立索引, ...

随机推荐

  1. vue项目优化之路由懒加载

    const login = () =>import('@/views/login'); export default new Router({ routes:[ { path:'/login', ...

  2. 编码补充 daty 6

    ---恢复内容开始--- 1.  用id求内存地址 id 查询内存地址 name = 'alex' print(id(name)) li = [1,2,3] print(id(li)) 结果: 2. ...

  3. c++ opencv 3.2 +Mfc VS2015窗体显示图片方法

    本文仅涉及一些核心步骤,具体 OpenCV 的配置以及其他的细节问题,请参考 VS2010 / MFC + OpenCV 2.4.1打开图片. 1. 新建 MFC 对话框项目 基于对话框,不使用Uni ...

  4. HTTP.ContentType

    1. multipart/x-mixed-replace http://blog.dubbelboer.com/2012/01/08/x-mixed-replace.html

  5. vue 动态路由按需加载的三种方式

    在Vue项目中,一般使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hell ...

  6. node.js 进程崩溃处理

    process.on('uncaughtException', (err) => { console.error('有错误'); });

  7. 多字节字符集与Unicode字符集

    在计算机中字符通常并不是保存为图像,每个字符都是使用一个编码来表示的,而每个字符究竟使用哪个编码代表,要取决于使用哪个字符集(charset). 多字节字符集: 在最初的时候,Internet上只有一 ...

  8. C++/CLI学习入门

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxIAAAFlCAYAAAB/fN6bAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw

  9. tmp下莫名其妙生成root权限的缓存文件

    现象: 原因:跟ngnix.conf有关,跟tmp的owner有关

  10. Java界面编程—事件的种类

    Java处理事件相应的类和监听接口大多位于 awt 包中. 在 java.swing.event 包中有专门用于 swing 组件的事件类和监听接口. awt 事件类继承自 AWTEvent,其超类是 ...