1、Specification


  1. //查询条件List
  2. List<Predicate> predicateList = new ArrayList<Predicate>();
  3. Specification specification = new Specification() {
  4. @Override
  5. public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
  6. //root即是Join<>内部第一个泛型的类型,意思就是用SkuProduct与Picture通过SkuProduct的pictures左联
  7. Join<SkuProduct,Picture> skuProductPictureJoin = root.join("pictures",JoinType.INNER);
  8. //添加第1个查询条件:SkuProduct的code等于skuProduct.getCode(),然后将这个criteriaBuilder的条件添加到predicateList
  9. predicateList.add(criteriaBuilder.equal(root.get("code"),skuProduct.getCode()));
  10. //添加第2个条件
  11. predicateList.add(criteriaBuilder.notEqual(skuProductPictureJoin.get("pictureType"),0));
  12. //返回
  13. return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
  14. }
  15. };
  16. //重要说明:Specification不支持右连接!
  17. //Specification specification = new Specification() {
  18. // @Override
  19. // public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
  20. // Join<Picture,SkuProduct> skuProductPictureJoin = root.join("pictures",JoinType.RIGHT);
  21. // predicateList.add(criteriaBuilder.notEqual(root.get("pictureType"),0));
  22. // predicateList.add(criteriaBuilder.equal(skuProductPictureJoin.get("code"),skuProduct.getCode()));
  23. // return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
  24. // }
  25. //};
  26. List<SkuProduct> results = skuProductRepository.findAll(specification);
  27. if(results!=null){
  28. results.stream().forEach(result->{
  29. System.out.println(result);
  30. });
  31. }

2、HQL


  1. @Query(value = "SELECT p FROM Picture p WHERE p.code= :code")
  2. List<Picture> findByPicture(@Param(value = "code") String code);

3、SQL

3.1--:占位符

  1. @Query(value = "SELECT p.* FROM picture p LEFT JOIN sku_product_pictures sp_p ON p.id=sp_p.pictures_id LEFT JOIN sku_product sp ON sp_p.sku_product_id=sp.id WHERE sp.code= :code AND p.picture_type=0",nativeQuery = true)
  2. List<Picture> findBySkuProductCodeAndPicture(@Param(value = "code") String code);

3.2--?占位符

  1. @Query(value = "SELECT p.* FROM picture p LEFT JOIN sku_product_pictures sp_p ON p.id=sp_p.pictures_id LEFT JOIN sku_product sp ON sp_p.sku_product_id=sp.id WHERE sp.code=?1 AND p.picture_type=?2",nativeQuery = true)
  2. List<Picture> findSkuProductCodeAndPicture(String code,int pictureType);


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://blog.csdn.net/trusause/article/details/78672595

JPA查询之Specification以及HQL、SQL查询的更多相关文章

  1. SAP 查询分析器,查询报表自动生成,SQL查询测试实现说明(转)

    在日常的SAP开发和应用中,经常需要通过查询SAP数据表来处理日常业务,比如:数据对账.报表SQL测试.SAP查询功能开发等.通过开发SAP查询分析器,SAP实施和开发人员,可以在较短的时间内查询到需 ...

  2. Hibernate SQL查询 addScalar()或addEntity()

    本文完全引用自: http://www.cnblogs.com/chenyixue/p/5601285.html Hibernate除了支持HQL查询外,还支持原生SQL查询.          对原 ...

  3. Hibernated的sql查询

    记录一下学习Hibernate的心得 1.为什么HIbernate会支持原生态的sql查询? HQL查询语句虽然方便我们查询,但是基于HQL的查询会将查询出来的对象保存到hibernate的缓存当中, ...

  4. Hibernate5.2之原生SQL查询

    Hibernate5.2之原生SQL查询 一. 介绍  在上一篇博客中笔者通过代码的形式给各位读者介绍了Hibernate中最重要的检索方式--HQL查询.在本博文中笔者将向各位读者介绍Hiberna ...

  5. SQL查询 addScalar()或addEntity()

    Hibernate除了支持HQL查询外,还支持原生SQL查询.   对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口.该 ...

  6. 13.hibernate的native sql查询(转自xiaoluo501395377)

    hibernate的native sql查询   在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hi ...

  7. Hibernate 的原生 SQL 查询

    Hibernate除了支持HQL查询外,还支持原生SQL查询.         对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取 ...

  8. Hibernate SQL查询 addScalar()或addEntity()【转】

    本文完全引用自: http://www.cnblogs.com/chenyixue/p/5601285.html Hibernate除了支持HQL查询外,还支持原生SQL查询.          对原 ...

  9. hibernate的native sql查询

    在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hibernate推荐的HQL查询语句呢?这是因为HQ ...

随机推荐

  1. sas信用评分之第二步变量筛选

    sas信用评分之第二步变量筛选 今天介绍变量初步选择.这部分的内容我就只介绍information –value,我这次做的模型用的逻辑回归,后面会更新以基尼系数或者信息熵基础的筛选变量,期待我把. ...

  2. ConvertJavaMiliSecondToDateTime

    private static DateTime ConvertJavaMiliSecondToDateTime(long javaMS)        {            DateTime UT ...

  3. python 正则表达式语法

  4. qbao

    # -*- coding: utf-8 -*- import Image, cStringIO, webbrowser, re, time, math import urllib, urllib2, ...

  5. 「WC2018」即时战略

    「WC2018」即时战略 考虑对于一条链:直接随便找点,然后不断问即可. 对于一个二叉树,树高logn,直接随便找点,然后不断问即可. 正解: 先随便找到一个点,问出到1的路径 然后找别的点,考虑问出 ...

  6. Codeforces 276D

    题目链接 这题真的体现了自己思维的不足,考虑问题只是考虑他的特殊性,却不能总结出它的一般性规律. 对于这题, 如果L == R , 那么结果为0. 否则, 我们只需要找到最高的某一位 (二进制数中的某 ...

  7. LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element

    二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...

  8. SPA是什么?

    认识SPA 最早单页面的应用无从知晓,在2004年,google的Gmail就使用了单页面.到了2010年,随着Backbone的问世之后,此概念才慢慢热了起来. 随着后来React.Angular. ...

  9. Libevent:4event loop

    一:运行loop       一旦一些events在event_base注册之后(下一节会讨论如何创建和注册events),就可以使Libevent等待events,并且在events准备好时能够通知 ...

  10. MaxCompute 项目子账号做权限管理

    场景: 一个企业使用多款阿里云产品,MaxCompute是其中一个产品,用的是同个主账号,主账号不是由使用MaxCompute的大数据同学管理,  大数据同学使用的是子账号.大数据同学日常需要给Max ...