1、Specification

    1. //查询条件List
      1. List<Predicate> predicateList = new ArrayList<Predicate>();
          1. Specification specification = new Specification() {
            1. @Override
              1. public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                1. //root即是Join<>内部第一个泛型的类型,意思就是用SkuProduct与Picture通过SkuProduct的pictures左联
                  1. Join<SkuProduct,Picture> skuProductPictureJoin = root.join("pictures",JoinType.INNER);
                    1. //添加第1个查询条件:SkuProduct的code等于skuProduct.getCode(),然后将这个criteriaBuilder的条件添加到predicateList
                      1. predicateList.add(criteriaBuilder.equal(root.get("code"),skuProduct.getCode()));
                        1. //添加第2个条件
                          1. predicateList.add(criteriaBuilder.notEqual(skuProductPictureJoin.get("pictureType"),0));
                            1. //返回
                              1. return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
                                1. }
                                  1. };
                                      1. //重要说明:Specification不支持右连接!
                                        1. //Specification specification = new Specification() {
                                          1. // @Override
                                            1. // public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                                              1. // Join<Picture,SkuProduct> skuProductPictureJoin = root.join("pictures",JoinType.RIGHT);
                                                1. // predicateList.add(criteriaBuilder.notEqual(root.get("pictureType"),0));
                                                  1. // predicateList.add(criteriaBuilder.equal(skuProductPictureJoin.get("code"),skuProduct.getCode()));
                                                    1. // return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
                                                      1. // }
                                                        1. //};
                                                            1. List<SkuProduct> results = skuProductRepository.findAll(specification);
                                                              1. if(results!=null){
                                                                1. results.stream().forEach(result->{
                                                                  1. System.out.println(result);
                                                                    1. });
                                                                      1. }
                                                                    1.  

                                                                    2、HQL

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

                                                                      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)
                                                                          1. List<Picture> findBySkuProductCodeAndPicture(@Param(value = "code") String code);
                                                                        1.  

                                                                        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)
                                                                            1. List<Picture> findSkuProductCodeAndPicture(String code,int pictureType);
                                                                          1.  


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

                                                                          原文地址: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. NOIP2016提高A组模拟9.28总结

                                                                            这次三道题都是可以AC的. 每道题思路都正确,但每道题都有细节没有注意. 第一题 1.没注意系数为1时可以省略系数: 2.没注意在第一项处理常数后,不能输出+号. 导致丢失20分:一定要多出特殊数据, ...

                                                                          2. iOS:你App的设置做对了吗?

                                                                            http://www.cocoachina.com/ios/20151217/14707.html iOS 8及以上版本最不为人知的一个特点是与应用设置的深层链接,用户可以根据APP的需要授权启用位置 ...

                                                                          3. ListView组件中 onEndReached 方法在滚动到距离列表最底部一半时执行

                                                                            初次使用ListView,在写列表滚动到最底部自动加载使用到方法onEndReached, 发现: ListView组件中 onEndReached 方法在滚动到距离列表最底部一半时执行, 于是翻看文 ...

                                                                          4. Precision和Recall

                                                                            学习自: http://blog.csdn.net/wangran51/article/details/7579100

                                                                          5. 《第一行代码》之——1.Android简介

                                                                            Android简介 Android系统架构 (图片源自维基百科) Android大致分为四层架构,五块区域. Linux内核层 Android系统基于Linux2.6,这一层为Android设备的各种 ...

                                                                          6. hdu2516 博弈

                                                                            找规律,发现时斐波那契数列:打表上. #include<stdio.h> #include<string.h> #define maxn 2147483647 __int64 ...

                                                                          7. hdu1527 威佐夫博奕

                                                                            有2堆石子,有2个人,每个人可以从一堆取或从2堆取一样的个数的石子,至少取1个.问先手的是胜或输.设(ak,bk)我么成为局势. (0,0)(1,2)(3,5)(4,7)..这种先手必输的叫奇异局势. ...

                                                                          8. 正则表达式问题:如何理解/href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))/(转载)

                                                                            ms-help://MS.VSCC/MS.MSDNVS.2052/jscript7/html/jsjsgrpregexpsyntax.htm 该文虽有解释, 但没有样例,对我这样的初学者来说很难理解 ...

                                                                          9. android去掉layout顶部的阴影(状态栏下边的阴影)

                                                                            http://blog.csdn.net/kepoon/article/details/7207100

                                                                          10. jQuery学习笔记之可见性过滤选择器

                                                                            可见性过滤选择器是根据元素的可见和不可见状态来选择相应的元素. 显示隐藏的例子: <!DOCTYPE html> <html> <head> <script ...