<tfoot id='0gRQI'></tfoot>

    <i id='0gRQI'><tr id='0gRQI'><dt id='0gRQI'><q id='0gRQI'><span id='0gRQI'><b id='0gRQI'><form id='0gRQI'><ins id='0gRQI'></ins><ul id='0gRQI'></ul><sub id='0gRQI'></sub></form><legend id='0gRQI'></legend><bdo id='0gRQI'><pre id='0gRQI'><center id='0gRQI'></center></pre></bdo></b><th id='0gRQI'></th></span></q></dt></tr></i><div id='0gRQI'><tfoot id='0gRQI'></tfoot><dl id='0gRQI'><fieldset id='0gRQI'></fieldset></dl></div>

      <legend id='0gRQI'><style id='0gRQI'><dir id='0gRQI'><q id='0gRQI'></q></dir></style></legend>
        • <bdo id='0gRQI'></bdo><ul id='0gRQI'></ul>

        <small id='0gRQI'></small><noframes id='0gRQI'>

      1. Spring Data JPA - 结果中具有多个聚合函数的自定义查询

        Spring Data JPA - Custom Query with multiple aggregate functions in result(Spring Data JPA - 结果中具有多个聚合函数的自定义查询)
        <tfoot id='Cyzi9'></tfoot>

          1. <i id='Cyzi9'><tr id='Cyzi9'><dt id='Cyzi9'><q id='Cyzi9'><span id='Cyzi9'><b id='Cyzi9'><form id='Cyzi9'><ins id='Cyzi9'></ins><ul id='Cyzi9'></ul><sub id='Cyzi9'></sub></form><legend id='Cyzi9'></legend><bdo id='Cyzi9'><pre id='Cyzi9'><center id='Cyzi9'></center></pre></bdo></b><th id='Cyzi9'></th></span></q></dt></tr></i><div id='Cyzi9'><tfoot id='Cyzi9'></tfoot><dl id='Cyzi9'><fieldset id='Cyzi9'></fieldset></dl></div>

              <legend id='Cyzi9'><style id='Cyzi9'><dir id='Cyzi9'><q id='Cyzi9'></q></dir></style></legend>
                  <tbody id='Cyzi9'></tbody>
                  <bdo id='Cyzi9'></bdo><ul id='Cyzi9'></ul>

                  <small id='Cyzi9'></small><noframes id='Cyzi9'>

                • 本文介绍了Spring Data JPA - 结果中具有多个聚合函数的自定义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图在一个查询中返回一组评分的平均值和计数.在我发现浏览的示例之后,我在两个查询中相当容易地管理它.例如:

                  I was trying to return an average and count of a set of ratings in one query. I managed it fairly easily in two queries following the example I found browsing. For example:

                  @Query("SELECT AVG(rating) from UserVideoRating where videoId=:videoId")
                  public double findAverageByVideoId(@Param("videoId") long videoId);
                  

                  但只要我想在同一个查询中计算平均值和计数,问题就开始了.经过几个小时的实验,我发现这很有效,所以我在这里分享它.希望对你有帮助.

                  but as soon as I wanted an average and a count in the same query, the trouble started. After many hours experimenting, I found this worked, so I am sharing it here. I hope it helps.

                  1) 我需要一个新的类来获得结果:

                  1) I needed a new class for the results:

                  我必须在查询中引用该类:

                  The I had to reference that class in the query:

                  @Query("SELECT new org.magnum.mobilecloud.video.model.AggregateResults(AVG(rating) as rating, COUNT(rating) as TotalRatings) from UserVideoRating where videoId=:videoId")
                  public AggregateResults findAvgRatingByVideoId(@Param("videoId") long videoId);
                  

                  一个查询现在返回平均评分和评分计数

                  One query now returns average rating and count of ratings

                  推荐答案

                  自己解决了.

                  自定义类接收结果:

                  public class AggregateResults {
                  
                      private final double rating;
                      private final int totalRatings;
                  
                      public AggregateResults(double rating, long totalRatings) {
                          this.rating = rating;
                          this.totalRatings = (int) totalRatings;
                      }
                  
                      public double getRating() {
                          return rating;
                      }
                  
                      public int getTotalRatings() {
                          return totalRatings;
                      }
                  }
                  

                  @Query("SELECT new org.magnum.mobilecloud.video.model.AggregateResults(
                      AVG(rating) as rating, 
                      COUNT(rating) as TotalRatings) 
                      FROM UserVideoRating
                      WHERE videoId=:videoId")
                  public AggregateResults findAvgRatingByVideoId(@Param("videoId") long videoId);
                  

                  这篇关于Spring Data JPA - 结果中具有多个聚合函数的自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <small id='Q7K7x'></small><noframes id='Q7K7x'>

                    <tbody id='Q7K7x'></tbody>

                  <tfoot id='Q7K7x'></tfoot>

                      <legend id='Q7K7x'><style id='Q7K7x'><dir id='Q7K7x'><q id='Q7K7x'></q></dir></style></legend>
                      • <bdo id='Q7K7x'></bdo><ul id='Q7K7x'></ul>
                          <i id='Q7K7x'><tr id='Q7K7x'><dt id='Q7K7x'><q id='Q7K7x'><span id='Q7K7x'><b id='Q7K7x'><form id='Q7K7x'><ins id='Q7K7x'></ins><ul id='Q7K7x'></ul><sub id='Q7K7x'></sub></form><legend id='Q7K7x'></legend><bdo id='Q7K7x'><pre id='Q7K7x'><center id='Q7K7x'></center></pre></bdo></b><th id='Q7K7x'></th></span></q></dt></tr></i><div id='Q7K7x'><tfoot id='Q7K7x'></tfoot><dl id='Q7K7x'><fieldset id='Q7K7x'></fieldset></dl></div>