【问题标题】:Spring data r2dbc and group bySpring数据r2dbc和分组依据
【发布时间】:2021-01-23 19:21:13
【问题描述】:

我正在使用 DatabaseClient 执行我的 sql 查询,但我不知道如何通过以下方式进行分组:

databaseClient.select()
              .from( myClass.class )
              .matching( where( "row_1" ).is( "value_1" ) )
              //group by row_2 ...
              .orderBy( Sort.Order.desc( "row_3" ) )
              .page( pageable  )
              .fetch()
              .all();

【问题讨论】:

    标签: spring-data spring-data-r2dbc r2dbc


    【解决方案1】:

    直接使用 sql 的变通解决方案。

    
        public Flux<Map<Object, Object>> countByStatus() {
            return this.databaseClient
                    .sql("SELECT count(*) as cnt, status FROM posts group by status")
                    .map((row, rowMetadata) -> {
                        Long cnt = row.get("cnt", Long.class);
                        Post.Status s = row.get("status", Post.Status.class);
    
                        return Map.<Object, Object>of("cnt", cnt, "status", s);
                    })
                    .all();
        }
    

    检查complete codes

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多