JPA 动态查询

  • JPA Repository 继承JpaSpecificationExecutor
public interface VwUserEstateRepository extends CrudRepository<VwUserEstate, Integer> , JpaSpecificationExecutor<VwUserEstate> {
  • 写个方法处理动态查询
protected static Specification<VwUserEstate> queryNotSendByTerms(Integer projectId, Integer materialCodeId, Integer batchId, Integer estateTypeId, Integer estateSubTypeId, List<Integer> estateIds, Integer uid) {
        return new Specification<VwUserEstate>() {
            public Predicate toPredicate(Root<VwUserEstate> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Predicate predicate = builder.conjunction();

                if (projectId != null) {
                    predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.projectId), projectId));
                }

                if (materialCodeId != null) {
                    predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.materialCodeId), materialCodeId));
                }

                if (batchId != null) {
                    predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.batchId), batchId));
                }

                if (estateTypeId != null) {
                    predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.estateTypeId), estateTypeId));
                }

                if (estateSubTypeId != null) {
                    predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.estateSubTypeId), estateSubTypeId));
                }

                Iterator iterator = estateIds.iterator();
                In in = builder.in(root.get(VwUserEstate_.id));
                while (iterator.hasNext()) {
                    in.value(iterator.next());
                }
                predicate.getExpressions().add(builder.not(in));


                predicate.getExpressions().add(builder.equal(root.get(VwUserEstate_.uid), uid));

                return predicate;
            }
        };
    }
  • 调用动态查询
            Page<VwUserEstate> list = vwUserEstateRepository.findAll(where(queryNotSendByTerms(projectId, materialCodeId, batchId, estateTypeId, estateSubTypeId, estateIds, uid)), page);
Spring Data JPA 提供了强大的动态查询能力,允许开发者在运行时构建查询,而不需要硬编码SQL语句。这种动态查询的实现通常是通过使用Spring Data JPA的Repository接口中的特定方法来完成的。 在Spring Data JPA中,可以通过以下几种方式实现动态查询: 1. 使用JPA Specification:通过定义一个Specification对象来描述查询条件,然后通过Repository接口的`findAll()`方法传递这个Specification对象。Specification允许开发者使用一系列的谓词组合成复杂的查询逻辑。 2. 使用Spring Data JPA的QueryDSL集成:QueryDSL提供了一种类型安全的方式来构建查询,它允许开发者使用流畅的API来构建查询,而不是编写字符串形式的查询语句。 3. 使用Criteria API:通过Criteria API可以构建类型安全的查询,并且可以动态地构建查询条件。Criteria API是Java持久化API的一部分,提供了一种程序化的查询语言,可以避免SQL注入等安全问题。 4. 使用JPQL(Java Persistence Query Language)或原生SQL查询:在某些复杂的查询场景下,可以使用JPQL来构建查询语句,或者直接使用原生SQL语句。 示例代码(使用Specification动态查询): ```java public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> { } public class UserSpecification { public static Specification<User> hasNameLike(final String name) { return (Specification<User>) (root, query, cb) -> cb.like(root.get("name"), "%" + name + "%"); } } // 在业务逻辑中使用 List<User> users = userRepository.findAll(UserSpecification.hasNameLike("张三")); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值