春季数据r2dbc和分组依据

时间:2020-10-08 17:24:58

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

我正在使用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();

1 个答案:

答案 0 :(得分:0)

直接使用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