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. LDAP Authentication Handler

    Including the Handler In the pom.xml file for your CAS Maven2 WAR Overlay, add the following depende ...

  2. Java ANSI转码UTF-8

    public static void change(String filepath) throws UnsupportedEncodingException, IOException{ Buffere ...

  3. Directx11教程(53) D3D11管线(8) GS的调度执行

    原文:Directx11教程(53) D3D11管线(8) GS的调度执行        在前面的教程中,我们分析了VS-PS的shader管线组合执行过程,本章我们分析一下VS-GS-PS的管线执行 ...

  4. PLAY2.6-SCALA(四) 请求体解析器

    一个http请求是一个请求头后面跟着一个请求体,头部信息比较短,可以安全的缓存在内存中,在Play中头部信息使用RequestHeader类进行建模.请求体的内容可能较大,使用流stream的形式进行 ...

  5. controller接收前台数据—中文乱码问题

    项目用的开发环境为tomcat+eclipse+SSM 正如题目,controller接收前台数据-中文乱码问题,在页面编码为UTF-8的前提下,解决方案有二: 一) controller接收数据时, ...

  6. 2019-9-2-win10-uwp-右击浮出窗在点击位置

    title author date CreateTime categories win10 uwp 右击浮出窗在点击位置 lindexi 2019-09-02 12:57:38 +0800 2018- ...

  7. VelocityTracker监控速度!!!

    用来追踪触摸事件(flinging事件和其他手势事件)的速率.用obtain()函数来获得类的实例,用addMovement(MotionEvent)函数将motion event加入到Velocit ...

  8. keep-alive vue组件缓存避免多次加载相应的组件

    keep-alive vue组件缓存避免多次加载相应的组件

  9. 选用适合的ORACLE优化器

    ORACLE的优化器共有3种: a.  RULE (基于规则)   b. COST (基于成本)  c. CHOOSE (选择性) 设置缺省的优化器,可以通过对init.ora文件中OPTIMIZER ...

  10. oracle函数 NLS_LOWER(x[,y])

    [功能]返回字符串并将字符串的变为小写; [参数]x字符型表达式 [参数]Nls_param可选,指定排序的方式(nls_sort=) . SCHINESE_RADICAL_M(部首.笔画) SCHI ...