【问题标题】:How to return Spring Specification<T> if requested object and field object have different classes?如果请求的对象和字段对象具有不同的类,如何返回 Spring Specification<T>?
【发布时间】:2022-01-08 17:40:18
【问题描述】:

@Entity Player 有字段:生日日期

控制器有方法:

 @GetMapping("/rest/players")
public ResponseEntity<List<Player>> findAll(@RequestParam(required = false) Long after) {
    //response
}

我想创建一个方法来返回某个日期之后生日的玩家规范,该日期即将到来:

private static Specification<Player> getPlayerByAfter(final Long after) {
    return ((root, query, criteriaBuilder) ->
            criteriaBuilder.greaterThan(criteriaBuilder.lower(root.get("birthday")), ));
}

我该怎么做?我对Spring一点也不熟悉,刚开始学习它。我试图用谷歌搜索它,但示例总是带有一些 @RequestParam String 并且我无法找到其他一些对象的任何内容,特别是如果它们具有不同的类。

谢谢你:)

【问题讨论】:

    标签: java spring spring-data-jpa spring-data


    【解决方案1】:

    好的,我找到了解决办法:

    private static Specification<Player> getPlayerByBefore(final Long before) {
        return ((root, query, criteriaBuilder) ->
                criteriaBuilder.lessThanOrEqualTo(root.get("birthday"), new Date(before)));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 2021-10-16
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多