Spring Data JPA动态查询(多条件and)

entity:

@Entity
@Table(name = "data_illustration")
public class Test {
    
    @Id
    @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "uuid")
    private String id;

    @Column(name = "fileId")
    private String fileid;

    private String title;

    @Column(name = "WFState")
    private String wfstate;

    @Column(name = "issueNo")
    private String issueno;

    private String format;

        
    //对应得set、get方法省略
          
}

mapper:

public interface IllustrationMapper extends JpaRepository<Test, String> {
    @Transactional
    @Modifying
    @Query(value="delete from test where id=?1 ",nativeQuery=true)
    int deleteByPrimaryKey(String id);

    
    List<Test> findAll(Specification<CsdbDataIllustration> specification);   //传入Specification对象


}

service:

public List<Test > getDataIllustrationList(Test test) {
        List<Test > test2 = dataIllustrationMapper.findAll(new Specification<Test >(){
            @Override
            public Predicate toPredicate(Root<Test > root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                
                if(StringUtils.isNotBlank(test.getId())){
                    predicates.add(cb.equal(root.get("id"), test.getId()));
                }
                
                if(StringUtils.isNotBlank(test.getFileid())){
                    predicates.add(cb.equal(root.get("fileid"), test.getFileid() ));
                }
                if(StringUtils.isNotBlank(test.getTitle())){
                    predicates.add(cb.equal(root.get("title"), test.getTitle() ));
                }
                if(StringUtils.isNotBlank(test.getWfstate())){
                    predicates.add(cb.equal(root.get("wfstate"), test.getWfstate() ));
                }
                if(StringUtils.isNotBlank(test.getIssueno())){
                    predicates.add(cb.equal(root.get("issueno"), test.getIssueno() ));
                }
                if(StringUtils.isNotBlank(test.getFormat())){
                    predicates.add(cb.equal(root.get("format"), test.getFormat() ));
                }
             
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));  //将上面满足条件的项用and拼接起
                                                   //来进行查询,当然此处也可以改为or或者like等等,视情况而定
            }
            
        });
        return test2;
    }

 

转载于:https://www.cnblogs.com/Amaris-Lin/p/7459198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring Data JPA进行多条件组合条件查询时,可以通过使用Specification来实现。Specification是一个接口,我们可以自定义一个实现该接口的类,然后在查询方法中传入该Specification对象来指定查询条件。 首先,我们需要在仓库接口中定义一个方法,该方法接收一个Specification参数,并返回查询结果。如下所示: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { List<User> findAll(Specification<User> spec); } ``` 接下来,我们在Specification实现类中重写toPredicate方法,在该方法中使用CriteriaBuilder构建查询条件,并返回一个Predicate对象表示查询条件。例如,我们可以按用户名和年龄进行查询,如下所示: ```java public class UserSpecification implements Specification<User> { private String username; private int age; public UserSpecification(String username, int age) { this.username = username; this.age = age; } @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { List<Predicate> predicates = new ArrayList<>(); if (username != null) { predicates.add(criteriaBuilder.equal(root.get("username"), username)); } if (age != 0) { predicates.add(criteriaBuilder.equal(root.get("age"), age)); } return criteriaBuilder.and(predicates.toArray(new Predicate[0])); } } ``` 最后,我们可以在服务类中调用仓库方法并传入自定义的Specification对象来进行查询。例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getUsers(String username, int age) { UserSpecification spec = new UserSpecification(username, age); return userRepository.findAll(spec); } } ``` 这样,我们就可以根据传入的条件来进行组合条件查询了。当传入的条件为空时,不会加入到查询中。当传入的条件有值时,则会根据该条件进行查询。这样,就实现了多条件组合条件查询
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值