java – 用于元组列表的crudrepository findBy方法签名
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 用于元组列表的crudrepository findBy方法签名,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1989字,纯文字阅读大概需要3分钟。
内容图文
![java – 用于元组列表的crudrepository findBy方法签名](/upload/InfoBanner/zyjiaocheng/704/be413b72269d4c28b3682548fe47c3e7.jpg)
我有一个像这样的实体类:
@Entity
@Table(name = "CUSTOMER")
class Customer{
@Id
@Column(name = "Id")
Long id;
@Column(name = "EMAIL_ID")
String emailId;
@Column(name = "MOBILE")
String mobile;
}
如何使用crudrepository spring data jpa为下面的查询编写findBy方法?
select * from customer where (email, mobile) IN (("a@b.c","8971"), ("e@f.g", "8888"))
我期待着类似的东西
List<Customer> findByEmailMobileIn(List<Tuple> tuples);
我想从给定的对中获取客户列表
解决方法:
我认为这可以使用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]));
}
}
您的repo应该扩展接口JpaSpecificationExecutor< Customer>.
然后构造一个带有元组列表的规范,并将其传递给方法customerRepo.findAll(Specification< Customer>) – 它返回一个客户列表.
内容总结
以上是互联网集市为您收集整理的java – 用于元组列表的crudrepository findBy方法签名全部内容,希望文章能够帮你解决java – 用于元组列表的crudrepository findBy方法签名所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。