• 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();
    }





  • 相关阅读:
    linux重新编译内核
    无废话ubuntu 13.4w文件共享配置
    VB6关于判断模态窗体的问题
    在.NET中快速创建一个5GB、10GB或更大的空文件
    利用虚拟光驱实现 将WINDOWS文件供虚拟机中的UBUNTU共享
    论这场云盘大战,以及各网盘的优劣
    struts2 全局格式化,格式化时间,金钱,数字
    SQL SERVER 2000/2005/2008数据库数据迁移到Oracle 10G细述
    女攻城师走在移动互联网道路的这两年
    用正则匹配多行文本
  • 原文地址:https://www.cnblogs.com/zhaoblog/p/6609824.html
Copyright © 2020-2023  润新知