Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据JPA通过可选变量查找_Java_Rest_Spring Data_Spring Data Jpa - Fatal编程技术网

Java Spring数据JPA通过可选变量查找

Java Spring数据JPA通过可选变量查找,java,rest,spring-data,spring-data-jpa,Java,Rest,Spring Data,Spring Data Jpa,我知道我可以通过向我的存储库界面添加一个方法来进行自定义查询。例如,如果我有Person实体 @Entity public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String firstName; private String lastName; private String gender;

我知道我可以通过向我的存储库界面添加一个方法来进行自定义查询。例如,如果我有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);

}
例如,如果任何正在使用RESTAPI的东西只想搜索firstName和lastName,它就会调用

http://localhost/people/search/findBy?firstName=John&firstName=Smith
更新:我能够解决这个问题,通过PersonRepository界面中的一些SQL技巧,我做了如下操作:

@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);
@Query(“从个人p中选择*,其中(:firstName为NULL)或(:firstName为非NULL且p.firstName=:firstName))和(:lastName为NULL)或(:lastName为非NULL且p.lastName=:lastName))和(:gender为NULL且p.gender=:gender))和(:dateOfBirth为NULL)或(:dateOfBirth不为NULL,p.dateOfBirth=:dateOfBirth)))
通过(@Param(“firstName”)字符串firstName、@Param(“lastName”)字符串lastName、@Param(“性别”)字符串gender、@Param(“dateOfBirth”)字符串dateOfBirth查找页面,可分页页面);
终点就是这样称呼的


你应该得到一份名为“约翰”的所有人的名单。

通过示例查找。 我是为员工做的

存储库:

//跳过的行
导入org.springframework.data.domain.Example
//跳过的行
接口EmployeeRepository扩展了JpaRepository{
列出findAll(示例员工);
}
用法:

//使用所有可用的按键搜索准备员工密钥(在我的示例中为6)
employeekey=新employeekey();
键。设置字段1(“字段1_值”);
key.setField2(“field2_值”);
//设置剩余的4个字段
//创建新员工并设置搜索键
员工=新员工();
employee.setEmployeeKey(key);
//通过传递上述Employee对象的示例来调用findAll
列表结果=employeeRepository.findAll(例如(employee));
@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);
//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));