【问题标题】:Spring Data JPA findBy optional variables [duplicate]Spring Data JPA findBy可选变量[重复]
【发布时间】:2017-05-18 15:51:47
【问题描述】:

我知道我可以通过向我的存储库接口添加一个方法来进行自定义查询。例如,如果我有 Person 实体。

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;
    private String gender;
    private String dateOfBirth;

    // All applicable getters and setters
}

我的存储库界面可能如下所示:

    @RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

但是如果我想搜索所有参数和所有参数组合怎么办。例如,如果我想通过以下方式搜索:

  • 名字(单独)
  • 姓氏(单独)
  • 性别(单独)
  • dateOfBirth(单独)
  • 名字和姓氏
  • 名字和性别
  • 名字和出生日期
  • 姓氏和性别
  • 姓氏和出生日期
  • 性别和出生日期
  • 名字和姓氏以及性别
  • firstName 和 lastName 以及 dateOfBirth
  • firstName 和 lastName 以及 dateOfBirth 和性别

这是否意味着我必须在界面中创建所有这些存储库方法。有没有一种方法可以选择按所有参数进行搜索。类似于

findBy(String firstName, String lastName, String gender, String dateOfBirth)

如果使用 REST API 的任何东西都想只搜索 firstName 和 lastName,它会调用

http://localhost/people/search/findBy?firstName=John&firstName=Smith

更新:我能够通过一些 SQL 技巧来解决这个问题 在 PersonRepository 接口中我做了这样的事情:

@Query("SELECT * FROM person p WHERE ((:firstName IS NULL) OR (:firstName IS NOT NULL AND p.firstName = :firstName)) AND ((:lastName IS NULL) OR (:lastName IS NOT NULL AND p.lastName = :lastName)) AND ((:gender IS NULL) OR (:gender IS NOT NULL AND p.gender = :gender)) AND ((:dateOfBirth IS NULL) OR (:dateOfBirth IS NOT NULL AND p.dateOfBirth = :dateOfBirth )) ")
        Page<Person> findBy(@Param("firstName") String firstName, @Param("lastName") String lastName, @Param("gender") String gender, @Param("dateOfBirth") String dateOfBirth, Pageable page);

以这种方式调用端点http://localhost:8080/people/search/findBy?firstName=John&lastname=&gender=&dateOfBirth=

你应该得到一个名为“John”的人的列表

【问题讨论】:

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


    【解决方案1】:

    通过示例使用 find。 我已经为员工做了。

    存储库:

    //skipped lines    
    import org.springframework.data.domain.Example
    //skipped lines
    interface EmployeeRepository extends JpaRepository<Employee, EmployeeKey>{
        List<Employee> findAll(Example<Employee> employee);
    }
    

    用法:

    // Prepare Employee key with all available search by keys (6 in my case)
    EmplyeeKey key = new EmplyeeKey();
    key.setField1("field1_value");
    key.setField2("field2_value");
    //Setting remaining 4 fields
    
    // Create new Employee ans set the search key
    Employee employee = new Employee();
    employee.setEmployeeKey(key);
    
    
    // Call the findAll by passing an Example of above Employee object
    List<Employee> result = employeeRepository.findAll(Example.of(employee));
    

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 2016-09-12
      • 2019-06-03
      • 2020-06-22
      相关资源
      最近更新 更多