您的位置:首页 > 编程语言 > Java开发

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

2017-04-20 09:34 549 查看
最近项目用到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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-boot jpa