前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JPA实现动态查询

JPA实现动态查询

作者头像
小王不头秃
发布2024-06-19 15:21:48
1620
发布2024-06-19 15:21:48
举报
JPA实现动态查询

前言

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

代码

JPA

代码语言:javascript
复制
 List<Apply> findAll(Specification<Apply> specification);

service

代码语言:javascript
复制
 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的动态查询

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JPA实现动态查询
  • 前言
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档