Spring 数据 REST 使用 QueryDSL 绑定进行范围检查

Spring Data REST making range check with QueryDSL binding

我有这样的要求:

http://localhost:4000/services/querywithdsl?min=30&max=90

我的域实体仅包含 id :

@Entity
@Data
public class ServiceNumberPorting {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(updatable = false, nullable = false)
    private Long id;
}

还有我的带谓词的休息控制器

@GetMapping(value = "/services/querywithdsl")
    public ResponseEntity<List<ServiceNumberPortingMapper>>getServiceNumberPortingWithDsl(@QuerydslPredicate(root = ServiceNumberPorting.class) Predicate predicate) {
        ...

    }

存储库:

public interface ServiceNumberPortingRepository extends JpaRepository<ServiceNumberPorting, Long>,QuerydslPredicateExecutor<ServiceNumberPorting>,    QuerydslBinderCustomizer<QServiceNumberPorting>

我想使用 querydsl 像这样使用绑定构建查询

..where id>min and id< max

我找到的所有示例都是直接将查询参数映射到实体列,但在这个示例中,我的实体中只有 id,我想对具有不同名称的传入参数值进行范围检查比我的实体列。

但是,由于我的域不包含最小和最大列,因此我无法使用自定义绑定(例如使用 QueryDslBinderCustomizer):

bindings.bind(store.city).single((path, value) -> path.endsWith(value));

如何实现?

这是我浏览过的文档: spring data examples binding examples

我能想到的解决方案之一是:

在您的存储库中,我定义了一个方法 getValueLyingBetween 并编写了一个简单的查询:

public interface ServiceNumberPortingRepository extends JpaRepository...{
    @Query("SELECT m FROM <YOUR_TABLE_NAME> m where ( m.id >=:min and m.id <= :max )")
    public ServiceNumberPorting getValueLyingBetween(@Param("min") String min, @Param("max") String max);

}

然后您可以使用最小值和最大值简单地从您的控制器调用此存储库方法。

这是您要找的吗?