Specification 参数的用法

----in用法
@Override
    public List findAll(List allCode) {
        List all =repository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                predicate.getExpressions().add(cb.in(root.get("code")).value(allCode));  //第一种的使用
				
//                Expression exp = root.get("code");  //第二种使用
//                predicate.getExpressions().add(exp.in(allCode));

                query.where(predicate);

                return query.getRestriction();
            }
        });
        return all;
    }
	
---equal、like、greaterThanOrEqualTo、lessThanOrEqualTo、between用法
	@Override
    public Page query(Person person, PageVo pv) {
        Pageable page = PageRequest.of(pv.getPage() - 1, pv.getSize());
        Page sensitiveWords = repository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                predicate.getExpressions().add(cb.equal(root.get("isStudent"), false));
                if (person !=null){
                    if (person .getName() != null && person.getId() != 0){
                        predicate.getExpressions().add(cb.equal(root.get("name"),person.getName()));
                        query.where(cb.and(predicate));
                        query.orderBy(cb.desc(root.get("id").as(String.class)));
                    }
					 if (StringUtils.isNotBlank(person.getName())) {
                    predicate.getExpressions().add(cb.like(root.get("name"), "%" + person.getName().trim() + "%"));
                }
				if (StringUtils.isNotBlank(start)){
                   Date start_day = DatetimeUtil.toDate(start,DatetimeUtil.YYYY_MM_DD_HH_MM_SS);
                    predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("receiveDate").as(Date.class),start_day));
                }
                if (StringUtils.isNotBlank(end)){
                    Date end_day =DatetimeUtil.toDate(end,DatetimeUtil.YYYY_MM_DD_HH_MM_SS);
                    predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("receiveDate").as(Date.class),end_day));
                }
				
				 if(StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)){
                        Date start_date = DatetimeUtil.toDate(start, DatetimeUtil.YYYY_MM_DD_HH_MM_SS);
                        Date end_date = DatetimeUtil.toDate(end, DatetimeUtil.YYYY_MM_DD_HH_MM_SS);
                        predicate.getExpressions().add(cb.between(root.get("createDate"), start_date, end_date));
                    }
                }
                return query.getRestriction();
            }
        },page);
        return sensitiveWords;
    }
	
	
	---计算总数
	 long count = repository.count(new Specification() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                predicate.getExpressions().add(cb.equal(root.get("isStudent"), false));
                predicate.getExpressions().add(cb.equal(root.get("code"), code));

                query.where(cb.and(predicate));
                return query.getRestriction();
            }
        });

        return count;
    }

 

你可能感兴趣的:(IT,Specification)