spring data jpa Specification 例子


/**
 * 封裝查詢條件
 * 
 * @param baseQueryDTO
 * @return
 */
private Specification<ActivityBase> getSpecification(ActivityBaseQueryDTO baseQueryDTO) {
    return new Specification<ActivityBase>() {
        @Override
        public Predicate toPredicate(Root<ActivityBase> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Predicate predicate = cb.conjunction();
            if (StringUtils.isNotEmpty(baseQueryDTO.getQueryActivityName())) {
                predicate.getExpressions()
                        .add(cb.like(root.get("activityName"), "%" + baseQueryDTO.getQueryActivityName() + "%"));
            }
            if (StringUtils.isNotEmpty(baseQueryDTO.getQueryOrgCode())) {
                predicate.getExpressions().add(cb.equal(root.get("orgCode"), baseQueryDTO.getQueryOrgCode()));
            }
            if (baseQueryDTO.getQueryStartDt() != null && baseQueryDTO.getQueryStartDt() != null) {
                predicate.getExpressions().add(cb.between(root.get("startDt"), baseQueryDTO.getQueryStartDt(),
                        baseQueryDTO.getQueryStartDt()));
            }
            if (StringUtils.isNotEmpty(baseQueryDTO.getActivityType())){
                predicate.getExpressions().add(cb.equal(root.get("activityType"), baseQueryDTO.getActivityType()));
            }
            return predicate;
        }
    };
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM