(小白学习笔记)Springboot入门(一):Specification
2017-04-20 09:34
549 查看
最近项目用到jhipster,后台用到Springboot,作为一个没接触过这个的小白,略捉急,最近要写一个各种条件的查询(条件中包括模糊条件和多选条件等),开始是直接用@query写,却发现对于多选条件和条件不选(and条件为空不能查询所有)的处理方面不太会处理,最后发现了specification,可以拼接sql语句,迷茫几天的我终于找到希望
MandatoryReportRepository(我只是添加了继承JpaSpecificationExecutor,里面的方法是jdl导入自己生成的,department是MandatoryReport中的外键):
MandatoryReportResource中:
参考资料:
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
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
相关文章推荐
- SpringBoot学习笔记 入门
- Spring boot入门实例 简易登录(精通Spring+4.x++企业应用开发实战 学习笔记二)
- [Spring入门学习笔记][Spring Boot]
- Spring Boot 学习笔记(一)——入门
- Springboot入门学习笔记(二)
- [Spring入门学习笔记][Spring Boot]
- Spring Boot 学习笔记1——原理入门
- spring-boot入门学习笔记
- spring boot 学习笔记(入门篇)
- 【Spring】Spring学习笔记-01-入门级实例
- spring入门篇-学习笔记
- spring学习笔记 aop入门
- Spring4 学习笔记(1)-入门及 HelloWorld -(供自己学习)
- python小白入门学习笔记-爬虫入门
- Spring学习笔记(二):Spring在STS中的配置和入门例子
- Spring学习笔记之入门(一)
- Spring学习笔记-springMVC入门Demo
- Spring Boot学习笔记-快速示例
- Spring学习笔记(3)----------Spring快速入门
- Spring学习笔记----01. 入门知识,IoC/DI