(小白学习笔记)Springboot入门(一):Specification
来源:互联网 发布:linux下装flashplayer 编辑:程序博客网 时间:2024/06/06 13:18
最近项目用到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
0 0
- (小白学习笔记)Springboot入门(一):Specification
- SpringBoot学习(一)SpringBoot基础入门
- SpringBoot学习(一)入门
- springboot 学习笔记(一)
- springboot 学习笔记(一)
- springboot 学习笔记(一)
- 【小白】selenium入门(一)学习笔记+登录淘宝
- SpringBoot 入门(一)
- SpringBoot入门(一)
- SpringBoot入门(一)
- springboot入门(一)
- SpringBoot(一、入门)
- springboot笔记(一)
- Springboot入门(一)Hello Springboot
- Springboot入门学习(一)
- springboot学习一(环境搭建入门学习)
- SpringBoot学习笔记 入门
- springBoot 学习(一)
- 软件测试自学指南---从入门到精通V1.0
- 共享库(GCC选项_-Wl,-soname)
- 财务会计基础(四)会计账户
- selenium问题集
- 五种前端开发必备的调试技术
- (小白学习笔记)Springboot入门(一):Specification
- 300行代码揭密轮播插件核心代码的实现过程
- MCUXPresso IDE:用于NXP ARM Cortex-M微控制器的统一的Eclipse IDE
- 【IMWeb训练营作业】 todolist
- 探究Android四大组件之Service
- 聚合和组合的关系
- NodeJS安装+NPM配置
- android studio修改配置文件(.android .gradle .AndroidStudio)位置
- three.js 多面几何体进行多面贴图