【问题标题】:Dynamic where condition in CrudRepositoryCrudRepository 中的动态 where 条件
【发布时间】:2019-05-22 08:41:56
【问题描述】:

我在 Spring Boot 中有一个查询: 我正在使用 CrudRepository,我需要为项目中使用的条件的所有可能组合声明方法名称。我们可以在 CrudRepository 中声明一个可以接受动态 where 条件的方法吗?

假设我有一个名为“Student”的表,它有 20 列。我不想在 CrudRepository 中为这 20 个字段定义所有可能的方法名称组合。我需要一个通用方法,它可以接受一个 json 对象作为参数,并根据作为 json 对象传递的 where 条件返回记录。有什么解决方案或解决方法吗?

【问题讨论】:

  • 您可以在 StudentRepository 接口中定义 StudentCustomeRepository 接口并扩展 CrudRepository 和 StudentCustomeRepository。第二步,您将在 StudentCustomeRepositoryImpl 中使用 CriteriaBuilder
  • 或使用 querydsl core.extensions.queryds

标签: spring spring-boot


【解决方案1】:

我认为您要查找的内容称为Specifications。 这些允许更精确的查询。

很好地解释了here(看看 5. 使用 JPA 规范)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 2020-10-13
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多