码迷,mamicode.com
首页 > 编程语言 > 详细

扩展Spring Data QBE支持范围查询

时间:2019-06-30 23:12:42      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:optional   domain   framework   tps   new   实例   结合   ecif   lower   

Spring Data JPA提供了Query by Example (QBE) 查询技术,实现了动态条件查询,不必再写烦琐的条件判断。但QBE不支持范围查询,本文结合QBE和Specification实现了动态范围查询。

本文以汪云飞-Spring Data JPA实现动态条件与范围查询实例代码为基础修改,利用org.springframework.data.domain.Range替换了自定义实现,支持Matching Any,保持了原代码的基本结构。

实现代码

FieldRange

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;
    }
}

ExampleSpecification

提取Example的Specification。

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 final Example<T> example;

    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);
    }
}

RangeSpecification

import org.springframework.data.domain.Range;
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;

    public RangeSpecification(FieldRange<E> fieldRange) {
        this.fieldRange = fieldRange;
    }

    @Override
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        Range<E> range = fieldRange.getRange();
        if (!isBounded(range)) {
            return builder.conjunction();
        }

        Path<E> path = root.get(fieldRange.getField());
        Optional<E> lower = range.getLowerBound().getValue();
        Optional<E> upper = range.getUpperBound().getValue();

        if (isBetween(range)) {
            return builder.between(path, lower.get(), upper.get());
        }

        if (lower.isPresent()) {
            return builder.greaterThanOrEqualTo(path, lower.get());
        }

        return builder.lessThanOrEqualTo(path, upper.get());
    }

    private boolean isBounded(Range<E> range) {
        return range.getLowerBound().isBounded() || range.getUpperBound().isBounded();
    }

    private boolean isBetween(Range<E> range) {
        return range.getLowerBound().isBounded() && range.getUpperBound().isBounded();
    }
}

自定义Repository

WiselyRepository接口

import org.itrunner.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实现

import org.itrunner.repository.WiselyRepository;
import org.itrunner.repository.specifications.ExampleSpecification;
import org.itrunner.repository.specifications.FieldRange;
import org.itrunner.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> {

    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)

调用范围查询:

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);
}

扩展Spring Data QBE支持范围查询

标签:optional   domain   framework   tps   new   实例   结合   ecif   lower   

原文地址:https://blog.51cto.com/7308310/2415621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!