(小白学习笔记)Springboot入门(一):Specification

最近项目用到jhipster,后台用到Springboot,作为一个没接触过这个的小白,略捉急,最近要写一个各种条件的查询(条件中包括模糊条件和多选条件等),开始是直接用@query写,却发现对于多选条件和条件不选(and条件为空不能查询所有)的处理方面不太会处理,最后发现了specification,可以拼接sql语句,迷茫几天的我终于找到希望

MandatoryReportRepository(我只是添加了继承JpaSpecificationExecutor,里面的方法是jdl导入自己生成的,department是MandatoryReport中的外键):

@SuppressWarnings("unused")
public interface MandatoryReportRepository extends JpaRepository<MandatoryReport,Long> ,JpaSpecificationExecutor<MandatoryReport>{

    @Query("select distinct mandatoryReport from MandatoryReport mandatoryReport left join fetch mandatoryReport.departments")
    List<MandatoryReport> findAllWithEagerRelationships();

    @Query("select mandatoryReport from MandatoryReport mandatoryReport left join fetch mandatoryReport.departments where mandatoryReport.id =:id")
    MandatoryReport findOneWithEagerRelationships(@Param("id") Long id);
}

MandatoryReportResource中:

@PostMapping("/reports/select")
@Timed
   public ResponseEntity<List<MandatoryReport>> findBySelections(@ApiParam Pageable pageable,
                                                                 @RequestParam(value="title",required =false) String title,
                                                                 @RequestParam(value="occurPlace",required =false) String occurPlace,
                                                                 @RequestParam(value="occurTime",required =false) LocalDate occurTime,
                                                                 @RequestParam(value="eventLevel",required =false) String eventLevel,
                                                                 @RequestParam(value="reportStatu",required =false)  List<ReportStatu> reportStatu,
                                                                 @RequestParam(value="eventType",required =false) List<String> eventType,
                                                                 @RequestParam(value="departments",required =false) List<Long> departments){
       Specification<MandatoryReport> specification=new Specification<MandatoryReport>() {
           @Override
           public Predicate toPredicate(Root<MandatoryReport> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
              List<Predicate> list=new ArrayList<Predicate>();
              if(title!=null){
                  list.add(criteriaBuilder.like(root.get("title").as(String.class),"%"+title+"%"));//标题模糊查询
              }
              if(occurPlace!=null){
                  list.add(criteriaBuilder.like(root.get("occurPlace").as(String.class),"%"+occurPlace+"%"));//发生地点模糊查询
              }
              if(occurTime!=null){
                  list.add(criteriaBuilder.equal(root.get("occurTime").as(LocalDate.class),occurTime));//发生时间精确查询
              }
              if(eventLevel!=null){
                   list.add(criteriaBuilder.equal(root.get("eventLevel").as(String.class),eventLevel));//事件等级精确查询
              }
               if(reportStatu!=null){//报告状态查询(枚举类型,可多选)
                   CriteriaBuilder.In<ReportStatu> in = criteriaBuilder.in(root.<ReportStatu> get("reportStatu"));
                   for (ReportStatu status : reportStatu) {
                       in.value(status);
                   }
                   list.add(in);
               }
               if(eventType!=null){//事件类型查询(可多选)
                   CriteriaBuilder.In<String> in = criteriaBuilder.in(root.<String> get("eventType"));
                   for (String status : eventType) {
                       in.value(status);
                   }
                   list.add(in);
               }
               if(departments!=null){//MandatoryReport,Department为多对多关系,按照涉及到部门查询(可多选)
                   Join<MandatoryReport,Department> join = root.join("departments", JoinType.LEFT);
                   CriteriaBuilder.In<Long> in = criteriaBuilder.in(join.get("id"));
                   for ( Long status : departments) {
                       in.value(status);
                 }
                   list.add(in);
               }
               Predicate[] p=new Predicate[list.size()];
          criteriaQuery.where(criteriaBuilder.and(list.toArray(p)));
               criteriaQuery.distinct(true);//去除重复的结果(多对多可能产生重复的结果)
               criteriaQuery.orderBy(criteriaBuilder.desc(root.get("reportTime").as(LocalDate.class))); //添加排序的功能
               return criteriaQuery.getRestriction();
           }
       };
       final Page<MandatoryReport> page = mandatoryReportRepository.findAll(specification,pageable);
       HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/reports/select");
       return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
   }

参考资料:
1.很详细的Spring-data-jpa介绍(详细,多选条件in用法,多对多查询写法都有):http://www.cnblogs.com/dreamroute/p/5173896.html 先mark,以后继续看~

2.Specification查询,刚开始,看的这个(动态拼接多个条件查询):http://sishuok.com/forum/blogPost/list/7000.html

3.in用法使用(适用于多选条件时):http://www.cnblogs.com/mr-wuxiansheng/p/6596603.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值