<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 - 结果中具有多个聚合函数的自定义查询

        时间:2024-08-23
        <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 - 结果中具有多个聚合函数的自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:获取 Spring Data 上按日期排序的最后记录 下一篇:FeignClient超时如何解决

                  相关文章

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

                  <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>