Raj*_*esh 5 java collections spring tuples spring-data-jpa
我有一个这样的实体类:
@Entity
@Table(name = "CUSTOMER")
class Customer{
@Id
@Column(name = "Id")
Long id;
@Column(name = "EMAIL_ID")
String emailId;
@Column(name = "MOBILE")
String mobile;
}
Run Code Online (Sandbox Code Playgroud)
如何使用 crudrepository spring 数据 jpa 为以下查询编写 findBy 方法?
select * from customer where (email, mobile) IN (("a@b.c","8971"), ("e@f.g", "8888"))
Run Code Online (Sandbox Code Playgroud)
我期待像
List<Customer> findByEmailMobileIn(List<Tuple> tuples);
Run Code Online (Sandbox Code Playgroud)
我想从给定的配对中获取客户列表
小智 7
我认为这可以通过org.springframework.data.jpa.domain.Specification
. 你可以传递你的元组列表并以这种方式处理它们(不要在意元组不是实体,但你需要定义这个类):
public class CustomerSpecification implements Specification<Customer> {
// names of the fields in your Customer entity
private static final String CONST_EMAIL_ID = "emailId";
private static final String CONST_MOBILE = "mobile";
private List<MyTuple> tuples;
public ClaimSpecification(List<MyTuple> tuples) {
this.tuples = tuples;
}
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// will be connected with logical OR
List<Predicate> predicates = new ArrayList<>();
tuples.forEach(tuple -> {
List<Predicate> innerPredicates = new ArrayList<>();
if (tuple.getEmail() != null) {
innerPredicates.add(cb.equal(root
.<String>get(CONST_EMAIL_ID), tuple.getEmail()));
}
if (tuple.getMobile() != null) {
innerPredicates.add(cb.equal(root
.<String>get(CONST_MOBILE), tuple.getMobile()));
}
// these predicates match a tuple, hence joined with AND
predicates.add(andTogether(innerPredicates, cb));
});
return orTogether(predicates, cb);
}
private Predicate orTogether(List<Predicate> predicates, CriteriaBuilder cb) {
return cb.or(predicates.toArray(new Predicate[0]));
}
private Predicate andTogether(List<Predicate> predicates, CriteriaBuilder cb) {
return cb.and(predicates.toArray(new Predicate[0]));
}
}
Run Code Online (Sandbox Code Playgroud)
您的 repo 应该扩展 interface JpaSpecificationExecutor<Customer>
。
然后用元组列表构造一个规范并将其传递给该方法customerRepo.findAll(Specification<Customer>)
- 它返回一个客户列表。
归档时间: |
|
查看次数: |
3665 次 |
最近记录: |