JPA实现动态查询

2 篇文章 1 订阅

JPA实现动态查询

前言

之前使用jpa的时候一直感慨它的一些原来就有的方法很好用,一边不是很习惯这种不是xml写sql的方式,尤其在用习惯了mybatis之后,在使用jpa写动态查询的时候真的一头雾水,直到发现了Specification 这个神奇的东西,使用下来觉得他和mybatis plus的条件构造器很像,而且可以实现动态查询,特意记录一下

代码

JPA

 List<Apply> findAll(Specification<Apply> specification);

service

 public List<Apply> getAllDynamatic(Apply apply) {
        Specification<Apply> queryCondition = new Specification<Apply>() {
            @Override
            public Predicate toPredicate(Root<Apply> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                // 根据传递的对象来进行条件的构造
                if (apply.getId() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("id"), apply.getId()));
                }
                if (apply.getState() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("state"), apply.getState()));
                }
                if (apply.getAwardtype() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("awardtype"), apply.getAwardtype()));
                }
                if (apply.getStudentid() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("studentid"), apply.getStudentid()));
                }
                if (apply.getInfo() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("info"), apply.getInfo()));
                }
                if (apply.getName() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("name"), apply.getName()));
                }
                if (apply.getType() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("type"), apply.getType()));
                }
                if (apply.getTeacherid() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("teacherid"), apply.getTeacherid()));
                }
                if (apply.getTeacherstate() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("teacherstate"), apply.getTeacherstate()));
                }
                if (apply.getProcess() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("process"), apply.getProcess()));
                }
                if (apply.getProcess2() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("process2"), apply.getProcess2()));
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return applyRepos.findAll(queryCondition);
    }

这样就实现了jpa的动态查询

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王不头秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值