Spring Data JPA提供了Query by Example (QBE) 查詢技術(shù),實(shí)現(xiàn)了動(dòng)態(tài)條件查詢,不必再寫煩瑣的條件判斷。但QBE不支持范圍查詢,本文結(jié)合QBE和Specification實(shí)現(xiàn)了動(dòng)態(tài)范圍查詢。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、宣化網(wǎng)站維護(hù)、網(wǎng)站推廣。
本文以汪云飛-Spring Data JPA實(shí)現(xiàn)動(dòng)態(tài)條件與范圍查詢實(shí)例代碼為基礎(chǔ)修改,利用org.springframework.data.domain.Range替換了自定義實(shí)現(xiàn),支持Matching Any,保持了原代碼的基本結(jié)構(gòu)。源碼地址 https://github.com/sunjc/heroes-api
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Range.Bound;
import static org.springframework.data.domain.Range.Bound.inclusive;
public class FieldRange<T extends Comparable<T>> {
private String field;
private Range<T> range;
public FieldRange(String field, T lower, T upper) {
this.field = field;
this.range = of(lower, upper);
}
private Range<T> of(T lower, T upper) {
Bound<T> lowerBound = Bound.unbounded();
Bound<T> upperBound = Bound.unbounded();
if (lower != null) {
lowerBound = inclusive(lower);
}
if (upper != null) {
upperBound = inclusive(upper);
}
return Range.of(lowerBound, upperBound);
}
public String getField() {
return field;
}
public Range<T> getRange() {
return range;
}
}
提取Example的Specification,SimpleJpaRepository內(nèi)含有此類,是私有的。
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.Assert;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import static org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicate;
public class ExampleSpecification<T> implements Specification<T> {
private static final long serialVersionUID = 1L;
private final Example<T> example; //NOSONAR
public ExampleSpecification(Example<T> example) {
Assert.notNull(example, "Example must not be null!");
this.example = example;
}
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
return getPredicate(root, criteriaBuilder, example);
}
}
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.*;
import java.util.Optional;
public class RangeSpecification<T, E extends Comparable<E>> implements Specification<T> {
private FieldRange<E> fieldRange; //NOSONAR
public RangeSpecification(FieldRange<E> fieldRange) {
this.fieldRange = fieldRange;
}
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Optional<E> lower = fieldRange.getRange().getLowerBound().getValue();
Optional<E> upper = fieldRange.getRange().getUpperBound().getValue();
Path<E> path = root.get(fieldRange.getField());
if (lower.isPresent() && upper.isPresent()) {
return builder.between(path, lower.get(), upper.get());
}
if (lower.isPresent()) {
return builder.greaterThanOrEqualTo(path, lower.get());
}
if (upper.isPresent()) {
return builder.lessThanOrEqualTo(path, upper.get());
}
return null;
}
}
WiselyRepository接口
import org.itrunner.heroes.repository.specifications.FieldRange;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.util.List;
@NoRepositoryBean
public interface WiselyRepository<T, ID> extends JpaRepository<T, ID> { //NOSONAR
Page<T> findByExampleAndRange(Example<T> example, List<FieldRange<? extends Comparable>> fieldRanges, Pageable pageable);
List<T> findByExampleAndRange(Example<T> example, List<FieldRange<? extends Comparable>> fieldRanges);
}WiselyRepository實(shí)現(xiàn)
import org.itrunner.heroes.repository.WiselyRepository;
import org.itrunner.heroes.repository.specifications.ExampleSpecification;
import org.itrunner.heroes.repository.specifications.FieldRange;
import org.itrunner.heroes.repository.specifications.RangeSpecification;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.data.jpa.domain.Specification.where;
public class WiselyRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements WiselyRepository<T, ID> { //NOSONAR
public WiselyRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
}
@Override
public Page<T> findByExampleAndRange(Example<T> example, List<FieldRange<? extends Comparable>> fieldRanges, Pageable pageable) {
return findAll(specifications(example, fieldRanges), pageable);
}
@Override
public List<T> findByExampleAndRange(Example<T> example, List<FieldRange<? extends Comparable>> fieldRanges) {
return findAll(specifications(example, fieldRanges));
}
private Specification<T> specifications(Example<T> example, List<FieldRange<? extends Comparable>> fieldRanges) {
boolean allMatching = example.getMatcher().isAllMatching();
Specification<T> byExample = new ExampleSpecification<>(example);
List<Specification<T>> byRanges = getRangeSpecifications(fieldRanges);
return conjunction(byExample, byRanges, allMatching);
}
private List<Specification<T>> getRangeSpecifications(List<FieldRange<? extends Comparable>> fieldRanges) {
List<Specification<T>> rangeSpecifications = new ArrayList<>();
for (FieldRange fieldRange : fieldRanges) {
rangeSpecifications.add(new RangeSpecification<>(fieldRange));
}
return rangeSpecifications;
}
private Specification<T> conjunction(Specification<T> byExample, List<Specification<T>> byRanges, boolean allMatching) {
Specification<T> specification = where(byExample);
for (Specification<T> rangeSpecification : byRanges) {
if (allMatching) {
specification = specification.and(rangeSpecification);
} else {
specification = specification.or(rangeSpecification);
}
}
return specification;
}
}
啟用WiselyRepositoryImpl:
@EnableJpaRepositories(repositoryBaseClass = WiselyRepositoryImpl.class)調(diào)用范圍查詢:
public Page<Hero> findHeroes(Hero hero, Date startDate, Date endDate, int minAge, int maxAge, Pageable pageable) {
List<FieldRange<? extends Comparable>> fieldRanges = new ArrayList<>();
fieldRanges.add(new FieldRange<>("birthday", startDate, endDate));
fieldRanges.add(new FieldRange<>("age", minAge, maxAge));
return heroRepository.findByExampleAndRange(of(hero), fieldRanges, pageable);
}
當(dāng)前題目:擴(kuò)展SpringDataQBE實(shí)現(xiàn)動(dòng)態(tài)范圍查詢
網(wǎng)址分享:http://www.chinadenli.net/article44/iphgee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、域名注冊(cè)、網(wǎng)站策劃、做網(wǎng)站、標(biāo)簽優(yōu)化、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)