Java 如何在SpringDataJPA中编写接受四种参数组合的select查询
我尝试在用户搜索多个组合时显示结果,他可以通过以下方式搜索选项-Java 如何在SpringDataJPA中编写接受四种参数组合的select查询,java,sql,spring,spring-data-jpa,Java,Sql,Spring,Spring Data Jpa,我尝试在用户搜索多个组合时显示结果,他可以通过以下方式搜索选项- 国家 陈述 地区 zipCode 他可以搜索的示例组合有(国家、州)、(国家、地区)、(州、zipCode)等 我也在使用SpringDataJPA进行查询和分页 我是Spring Jpa的新手,任何帮助都将不胜感激 谢谢大家! 使用标准: public Collection<? extends Profile> get(Collection<? extends Integer> ids, boolean
public Collection<? extends Profile> get(Collection<? extends Integer> ids, boolean deleted) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Profile> q = cb.createQuery(Profile.class);
Root<Profile> r = q.from(Profile.class);
if (deleted) {
Predicate andClose = cb.and(cb.equal(r.get("deleted"), true));
q.where(r.get("id").in(ids), andClose);
} else {
q.where(r.get("id").in(ids));
}
q.orderBy(cb.asc(r.get("id")));
TypedQuery<Profile> query = em.createQuery(q);
return query.getResultList();
}
public Collection有一个非常简单的技巧来执行您需要的操作;)
如果您正在扩展JpaRepository接口并让Spring数据处理查询的连接和自动生成,那么您可以轻松地扩展接口以处理您的场景,而无需编写任何过于复杂的内容:
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
public List<Customer> findAllByCountryAndState(String country, String State);
}
公共接口CustomerRepository扩展了JpaRepository{
公共列表findAllByCountryAndState(字符串国家,字符串状态);
}
您可以按如下方式使用此方法:
@Autowired
private CustomerRepository customerRepository;
public void testMethod(){
List<Customer> customerList = customerRepository.findAllByCountryAndState("Canada","Ontario");
}
@Autowired
私人客户存储客户存储;
公共void testMethod(){
List customerList=customerRepository.findAllByCountryAndState(“加拿大”、“安大略省”);
}
Spring文档中有很多例子:您在搜索什么类型的对象?如果这是您的问题,结果将是一个列表。@sw-affairator;)
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
public List<Customer> findAllByCountryAndState(String country, String State);
}
@Autowired
private CustomerRepository customerRepository;
public void testMethod(){
List<Customer> customerList = customerRepository.findAllByCountryAndState("Canada","Ontario");
}