扩展Spring Data QBE支持范围查询

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

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

时间: 2024-10-05 23:26:06

扩展Spring Data QBE支持范围查询的相关文章

Spring Data Jpa之高级查询(jpa-spec插件)

友情链接:Spring Data Jpa的动态查询库 https://github.com/wenhao/jpa-spec 功能介绍 兼容Spring Data Jpa 和JPA2.1接口. Equal/NotEqual/Like/NotLike/In/NotIn支持可变参数, Equal/NotEqual 支持空(Null)值. 每个条件支持关联查询. 支持自定义条件查询. 条件构建器. 支持分页和排序. 导包: <dependency> <groupId>com.github.

Spring Data Rest 支持Put请求

最近在用 Spring Data Rest 写WebApi 遇到Put请求后报415 Mieda Type 的错误,GG了下 找到个解决办法,下面是针对Put请求的过滤. 1 public class HttpPutRestfulContentFilter extends OncePerRequestFilter { 2 3 private Charset charset = Charset.forName("UTF-8"); 4 private final String method

spring data jpa 一对多查询

在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键,这一列就是外键. package com.ytkj.entity; import javax.persistence.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * @Entity * 作用:

spring data jpa的动态查询封装(转)

最近使用spring data jpa做了两个项目,对于动态查询的不友好做了个类似hibernate的封装,记录也分享下 首先定义一个所有条件的容器,继承Specification Java代码   /** * 定义一个查询条件容器 * @author lee * * @param <T> */ public class Criteria<T> implements Specification<T>{ private List<Criterion> crit

Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 两种处理方法

简介:Spring Data JPA 其实就是JDK方式的动态代理 (需要一个接口 有一大堆最上边的是Repository接口来自org.springframework.data.repository,还有CrudRepository接口及一个实现类SimpleJpaRepository),只要有接口就可以查询数据库了,实际上就是proxy的方法,具体查询的方法有两种一种是简单式就是方法名为findBy+属性名+(AndOrIsEquals等)另一种是自定义的方法就是属性名是瞎起的向abc xy

记: Spring Data Jpa @OneToMany 级联查询被动触发的问题

I have encountered a bug in using Spring Data Jpa. Specifically,when @OneToMany was used to maintain a one-to-many relationship, lazy loading was effective.However,it may passively trigger the cascading query without using the cascading property. My

Spring Data JPA原生SQL查询

package com.wanda.cms.dao.repository;import org.springframework.stereotype.Repository;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.Query;import java.math.BigInteger;import java.util.*; /*

spring data elasticsearch多索引查询

一次查询多个索引数据 es里可以这样写 GET 索引1,索引2,索引3/_search 也可以这样 给索引创建别名,多个索引可以使用一个别名 POST /_aliases { "actions": [ { "add": { "index": "myindex2", "alias": "myalias" } },{ "add": { "index":

spring data jpa Specification 复杂查询+分页查询

当Repository接口继承了JpaSpecificationExecutor后,我们就可以使用如下接口进行分页查询: /** * Returns a {@link Page} of entities matching the given {@link Specification}. * * @param spec can be {@literal null}. * @param pageable must not be {@literal null}. * @return never {@l