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. Bnd教程(1):如何用命令行安装bnd

    1. 如果你用的是MacOS,请运行: brew install bnd 然后运行bnd version看是否安装成功: $ bnd version 4.0.0.201801191620-SNAPSH ...

  2. PLAY2.6-SCALA(十二) 表单的处理

    一.表单处理流程如下 1.定义一个表单,在这里表单最多有22个字段 import play.api.data._ import play.api.data.Forms._ //要使用验证和约束 imp ...

  3. Spring简介+HelloWorld

    IoC Container: 这是最重要的,也是最基础的, Spring的基础.它的作用是配置和Java对象的生命周期管理. DAO, ORM, AOP, WEB: 该模块可用于将工具或框架集成到了S ...

  4. NodeJs的简介及安装

    首先 分享一下NodeJs的应用场景吧: Web聊天室(IM):Express + Socket.io socket.io一个是基于Nodejs架构体系的,支持websocket的协议用于时时通信的一 ...

  5. 洛谷 P1505 [国家集训队]旅游 树链剖分

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 AC代码 总结 题面 题目链接 P1505 [国家集训队]旅游 题目描述 Ray 乐 ...

  6. php中括号定义数组

    php5.3及之前的版本是不支持中括号定义数组的.5.4之后支持. 错误信息是,不识别“[”

  7. [linux]jenkins迁移 标签: linux服务器 2016-08-28 21:29 988人阅读 评论(20)

    我们的测试的jenkins和开发的jenkins 是分开的两个jenkins,然后测试的jenkins很久没有用,我们打算把主节点搬到另外一条服务器上面,然后出了一系列的问题,如下: 一.安装jenk ...

  8. el-table中加载图片问题

    <el-table-column label="头像" width="100"> <template scope="scope&qu ...

  9. java代码简单实现栈

    1. 基于数组简单实现 /** * @author <a herf="mailto:yanwu0527@163.com">XuBaofeng</a> * @ ...

  10. TCP/IP,UDP,HTTP,SOCKET之间的区别和关系

    TCP/IP TCP/IP代表传输控制协议/网际协议,指的是一系列协组.可分为四个层次:数据链路层.网络层.传输层和应用层. 在网络层:有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协 ...