Java 如何使用随参数变化的积垢沉淀?

Java 如何使用随参数变化的积垢沉淀?,java,spring,spring-data-jpa,jpql,Java,Spring,Spring Data Jpa,Jpql,我想给大家看一张桌子。用户应该能够发送查询,并根据基本上是可选的属性进行筛选 问题:对于要筛选的每个属性,我必须在spring-data-jpa中使用'crudepository: public interface PersonRepository extends CrudRepository<Person, Long> { List<Person> findByFirstname(firstname); List<Person> findby

我想给大家看一张桌子。用户应该能够发送查询,并根据基本上是可选的属性进行筛选

问题:对于要筛选的每个属性,我必须在
spring-data-jpa
中使用'crudepository:

public interface PersonRepository extends CrudRepository<Person, Long> {
    List<Person> findByFirstname(firstname);
    List<Person> findbyFirstnameAndLastname(first, last);
    List<Person> findByFirstnameAndLastnameAndAge(first, last, age);
    List<Person> findByFirstnameAndLastnameAndAgeAndCity(first, last, age, city);
}
公共接口PersonRepository扩展了Crudepository{
列出findByFirstname(firstname);
列出FindByFirstName和LastName(第一个、最后一个);
列出findByFirstnameAndLastnameAndAge(第一个、最后一个、年龄);
列出查找到的第一个姓名和最后一个姓名以及年龄和城市(第一个、最后一个、年龄、城市);
}
问题:我怎样才能做得更好(不必写母语)
PreparedStatement
s我自己)

您使用分页和流查询来查询batter结果集需求实体

您只需要在实体上设置所需的属性,并将其传递给方法
findAll(实体)

例如,若您希望员工具有某些属性值,那个么就在其上设置参数,并传递给查询数据库

Employee employee = new Employee ();
employee. setAge (35);
employee. setExperience(5);
现在将其传递给查询

List <Employee> employèes = do. findAll (Employee);
List employeeèes=do。芬德尔(雇员);
允许通过将简单过滤器标准填充到“示例”实体中来创建过滤器:

公共接口PersonRepository扩展了Crudepository{
Iterable findAll(示例);
}
...
Person=新人();
person.setFirstname(“Dave”);
赛特城(“西雅图”);
Iterable davesFromSeattle=personRepository.findAll(例如,of(person));

作为一种变体,这会有所帮助:这真的有效吗?通过
@实体搜索
bean?那么,您接下来如何创建类似“byFirstnameIgnoreCase”或“byAgeGreaterThan”的查询呢?对于
忽略的案例
类似
开头,
结尾,您可以使用ExampleMather。对于数字范围,您可以尝试更复杂(但功能更强大)的
规范
方法。
public interface PersonRepository extends CrudRepository<Person, Long> {
    <S extends T> Iterable<S> findAll(Example<S> example);
}

...

Person person = new Person();
person.setFirstname("Dave");
person.setCity("Seattle");
Iterable<Person> davesFromSeattle = personRepository.findAll(Example.of(person));