zoukankan      html  css  js  c++  java
  • java-jpa-criteriaBuilder使用

    一个复杂的查询例子(包含常用的所有查询方法) 

         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         //查询结果所需要的类型(Entity相对应) CriteriaQuery
    <Entity> criteriaQuery = criteriaBuilder.createQuery(Entity.class);
         //查询所需要的主体类(Entity0相对应) Root
    <Entity0> root = criteriaQuery.from(Entity0.class);
         //查询结果-select(此处查询所有符合条件的主体类) criteriaQuery.select(root);
         //过滤条件用Predicate方法拼接 Predicate restrictions
    = criteriaBuilder.conjunction();
         //过滤条件——equal(当Entity0关联member类时,Entity0:member=m:1)
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); //过滤条件——like restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.like(root.<String>get("str"), "%"+str+"%")); //用户名查询(member里面的username匹配) ———— 多层查询 ———— 子查询的一种:适用于m:1或1:1(即多对一或一对一) restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.like(root.get("member").<String>get("username"), "%"+username+"%")); //子查询(规范写法,先判断查询内容是否存在)(适用于1:m)(即一对多) if (searchType != null || searchValue != null || hasExpired != null || status != null || type != null || isPendingReceive != null || isPendingRefunds != null || isAllocatedStock != null || businessType != null) { //建立子查询
           Subquery
    <Order> orderSubquery = criteriaQuery.subquery(Order.class); Root<Order> orderSubqueryRoot = orderSubquery.from(Order.class); orderSubquery.select(orderSubqueryRoot);
           //子查询和父查询相关联 Predicate orderRestrictions
    = criteriaBuilder.equal(orderSubqueryRoot.<MergeOrder>get("mergeOrder"), root); //子查询过滤条件拼接 if (searchType != null && searchValue != null) {if ("phone".equals(searchType)) { orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.like(orderSubqueryRoot.<String>get("phone"), "%"+searchValue+"%")); } }if (type != null) { CriteriaBuilder.In<Order.Type> in = criteriaBuilder.in(orderSubqueryRoot.<Order.Type>get("type")); in.value(type); orderRestrictions = criteriaBuilder.and(orderRestrictions, in); } //and、or以及判断是否为null,比较(>)的使用(比较可以用于日期比较) if (hasExpired != null) { orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.or(orderSubqueryRoot.get("expire").isNull(), criteriaBuilder.greaterThan(orderSubqueryRoot.<Date>get("expire"), new Date()))); } // not的使用方法(不符合上述过滤条件),notEqual的使用,<(小于)的使用 if (isPendingReceive != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery)); Predicate predicate = criteriaBuilder.and(criteriaBuilder.or(orderSubqueryRoot.get("expire").isNull() , criteriaBuilder.greaterThan(orderSubqueryRoot.<Date>get("expire"), new Date())) , criteriaBuilder.notEqual(orderSubqueryRoot.get("status"), Order.Status.completed) , criteriaBuilder.lessThan(orderSubqueryRoot.<BigDecimal>get("amountPaid"), orderSubqueryRoot.<BigDecimal>get("amount"))); if (isPendingReceive) { orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.not(predicate)); } }// 多层查询使用if (businessType != null) { orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.equal(orderSubqueryRoot.get("store").get("business").get("businessType"), businessType)); }
           // 拼接过滤条件 orderSubquery.where(orderRestrictions);
    // 和总条件拼接(exists的使用) restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderSubquery)); } criteriaQuery.where(restrictions);
         TypedQuery<Entity> query = entityManager.createQuery(criteriaQuery);
         Entity singleResult = query.getSingleResult();
  • 相关阅读:
    最有价值的专家--微软官方网站
    CC++初学者编程教程(16) 搭建Xcode cocos2dx开发环境--尹成老师博客
    微软最具价值的专家之毛星云博客
    Winsock IO模型之IOCP模型
    IOCP模型与网络编程
    关于proc索引的创建
    查找表存在于那些proc中
    Sql 时间做条件
    Sql 无指定条件,防并发update
    sql 根据客户需用 增减查询字段
  • 原文地址:https://www.cnblogs.com/g-smile/p/9177841.html
Copyright © 2011-2022 走看看