Spring 具有不同参数的数据 JPA 查询
Spring data JPA query with varying params
我正在尝试通过
等几个参数找到 table 的必要元素
List <Person> findByLastname(String lastname);
但是,如果这些参数中的一些将来会是 added/deleted 怎么办?如何使用 Spring Data JPA 使某些参数可选,类似这样
List <Person> findByOptionalLastnameAndOptionalFirstNameAnd...(String lastname, String firstname,...);
您不能使用 Spring 数据的可选参数和命名方法解析器,您应该为每种可能性创建方法,这就是为什么我建议您使用 Specification 来构建一个根据条件列表查询。
如果你不确定你的参数是否固定,那么我会使用这个方法:
使用 spring 规范,您可以根据某些条件构建查询。 https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
创建一些 JUinit 测试以确保您的查询稳定,如果有人 add/remove 某些参数应该修改 JUnit 以使其保持更新。
如果您对如何使用有任何疑问,请告诉我,我可以与您分享其他示例。
我想这 post 就是您要找的东西 :
您可以通过向查询添加多个谓词来获得基于输入的动态条件。
此参考可能也有用 Spring Data JPA Specifications
Yes it is possible but, then you need to make different queries
according to given parameters.
这就像:
@RequestMapping("find")
public List<Person> findByOptional...(@RequestParam(value = "fName", required = false) String fname,@RequestParam(value = "lName", required = false) String lname) {
//here you need to check you data for not null and create methods accordingly
}
*但这不是那么可取,如果你传递了更多的可选参数,那么需要添加更多的 if else 条件。
我的任务是通过在 build.gradle:
中使用 querydsl framework. If you use gradle, you will need to add this 解决的
dependencies {
compile "com.mysema.querydsl:querydsl-jpa:3.6.3"
compile "com.mysema.querydsl:querydsl-apt:3.6.3:jpa" // Magic happens here
compile "org.hibernate:hibernate-entitymanager:4.3.5.Final"
compile 'com.h2database:h2:1.4.187'
}
也可以使用 JPA Criteria API,很好的例子是 here and good tutorial is here。
@Entity
public class A {
@Id private Long id;
String someAttribute;
String someOtherAttribute;
...
}
查询本身:
//some parameters to your method
String param1 = "1";
String paramNull = null;
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery cq = qb.createQuery();
Root<A> customer = cq.from(A.class);
//Constructing list of parameters
List<Predicate> predicates = new ArrayList<Predicate>();
//Adding predicates in case of parameter not being null
if (param1 != null) {
predicates.add(
qb.equal(customer.get("someAttribute"), param1));
}
if (paramNull != null) {
predicates.add(
qb.equal(customer.get("someOtherAttribute"), paramNull));
}
//query itself
cq.select(customer)
.where(predicates.toArray(new Predicate[]{}));
//execute query and do something with result
em.createQuery(cq).getResultList();
使用实例查询
Person person = new Person();
person.setLastname("Smith");
Example<Person> example = Example.of(person);
List<Person> results = personRepository.findAll(example);
文档http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
这是 Adam Erstelle 对 Spring Data JPA: Query by Example?
的回答
我正在尝试通过
等几个参数找到 table 的必要元素List <Person> findByLastname(String lastname);
但是,如果这些参数中的一些将来会是 added/deleted 怎么办?如何使用 Spring Data JPA 使某些参数可选,类似这样
List <Person> findByOptionalLastnameAndOptionalFirstNameAnd...(String lastname, String firstname,...);
您不能使用 Spring 数据的可选参数和命名方法解析器,您应该为每种可能性创建方法,这就是为什么我建议您使用 Specification 来构建一个根据条件列表查询。
如果你不确定你的参数是否固定,那么我会使用这个方法:
使用 spring 规范,您可以根据某些条件构建查询。 https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
创建一些 JUinit 测试以确保您的查询稳定,如果有人 add/remove 某些参数应该修改 JUnit 以使其保持更新。
如果您对如何使用有任何疑问,请告诉我,我可以与您分享其他示例。
我想这 post 就是您要找的东西 :
您可以通过向查询添加多个谓词来获得基于输入的动态条件。
此参考可能也有用 Spring Data JPA Specifications
Yes it is possible but, then you need to make different queries according to given parameters.
这就像:
@RequestMapping("find")
public List<Person> findByOptional...(@RequestParam(value = "fName", required = false) String fname,@RequestParam(value = "lName", required = false) String lname) {
//here you need to check you data for not null and create methods accordingly
}
*但这不是那么可取,如果你传递了更多的可选参数,那么需要添加更多的 if else 条件。
我的任务是通过在 build.gradle:
中使用 querydsl framework. If you use gradle, you will need to add this 解决的dependencies {
compile "com.mysema.querydsl:querydsl-jpa:3.6.3"
compile "com.mysema.querydsl:querydsl-apt:3.6.3:jpa" // Magic happens here
compile "org.hibernate:hibernate-entitymanager:4.3.5.Final"
compile 'com.h2database:h2:1.4.187'
}
也可以使用 JPA Criteria API,很好的例子是 here and good tutorial is here。
@Entity
public class A {
@Id private Long id;
String someAttribute;
String someOtherAttribute;
...
}
查询本身:
//some parameters to your method
String param1 = "1";
String paramNull = null;
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery cq = qb.createQuery();
Root<A> customer = cq.from(A.class);
//Constructing list of parameters
List<Predicate> predicates = new ArrayList<Predicate>();
//Adding predicates in case of parameter not being null
if (param1 != null) {
predicates.add(
qb.equal(customer.get("someAttribute"), param1));
}
if (paramNull != null) {
predicates.add(
qb.equal(customer.get("someOtherAttribute"), paramNull));
}
//query itself
cq.select(customer)
.where(predicates.toArray(new Predicate[]{}));
//execute query and do something with result
em.createQuery(cq).getResultList();
使用实例查询
Person person = new Person();
person.setLastname("Smith");
Example<Person> example = Example.of(person);
List<Person> results = personRepository.findAll(example);
文档http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
这是 Adam Erstelle 对 Spring Data JPA: Query by Example?
的回答