如何通过使用 JPQL 的枚举 属性 或 spring 数据中的条件 Api 获取自定义数据组?
how can I get custom data group by enum property with JPQL or Criteria Api in spring data?
我有一个枚举来存储货币:
public enum Currency {
USD,EUR,GBP
}
我有包含货币的销售实体 属性:
public class Sales {
private Long id;
private Merchant merchant;
private Customer customer;
private double amount;
private Currency currency;
}
示例table 销售数据如下:
+----+-------------+-------------+--------+----------+
| id | merchant_id | customer_id | amount | currency |
+----+-------------+-------------+--------+----------+
| 1 | 2 | 3 | 125.0 | EUR |
| 2 | 1 | 2 | 135.0 | USD |
| 3 | 3 | 5 | 140.0 | GBP |
| 4 | 2 | 8 | 25.0 | USD |
| 5 | 4 | 8 | 95.0 | EUR |
| 6 | 6 | 9 | 85.0 | EUR |
| 7 | 5 | 6 | 90.0 | USD |
| 8 | 1 | 1 | 225.0 | EUR |
| 9 | 7 | 2 | 350.0 | GBP |
| . | . | . | . | . |
| . | . | . | . | . |
| . | . | . | . | . |
| . | . | . | . | . |
+----+-------------+-------------+--------+----------+
这是我的自定义数据模型
public class CustomDataModel{
private Integer count; //record count
private double total; // its total amount for specified currency
private Currency currency; // it also could be String, it is doesn't matter
}
我的查询参数:merchant_id 和 customer_id.
我想得到结果 List<CustomDataModel>
例如:
[
{
"count":5,
"total":674.00,
"currency":"USD"
},
{
"count":3,
"total":561.00,
"currency":"EUR"
},
{
"count":4,
"total":715.00,
"currency":"GBP"
}
]
有没有可能得到这样的结果。如果是我该怎么办?
我想你想要 List<CustomDataModel>
按货币分组。如果是这样,则无需依赖 merchant_id
或 customer_id
。
您可以在您的存储库中添加以下 @Query
方法,如下所示:
@Query("select new com.demo.spring.domain.CustomDataModel(count(s), sum(s.amount), s.currency) from Sales s group by s.currency")
List<CustomDataModel> getGroupByCurrency();
只需确保将 Integer 转换为 Long,就像在 CustomDataModel
class 中的 - private Integer count;
到 private Long count;
并向其添加以下构造函数:
public CustomDataModel(Long count, double total, Currency currency) {
super();
this.count = count;
this.total = total;
this.currency = currency;
}
如果你想按商户和客户分组,你可以将上面的group by子句更新为...group by s.currency, s.merchant, s.customer
我有一个枚举来存储货币:
public enum Currency {
USD,EUR,GBP
}
我有包含货币的销售实体 属性:
public class Sales {
private Long id;
private Merchant merchant;
private Customer customer;
private double amount;
private Currency currency;
}
示例table 销售数据如下:
+----+-------------+-------------+--------+----------+
| id | merchant_id | customer_id | amount | currency |
+----+-------------+-------------+--------+----------+
| 1 | 2 | 3 | 125.0 | EUR |
| 2 | 1 | 2 | 135.0 | USD |
| 3 | 3 | 5 | 140.0 | GBP |
| 4 | 2 | 8 | 25.0 | USD |
| 5 | 4 | 8 | 95.0 | EUR |
| 6 | 6 | 9 | 85.0 | EUR |
| 7 | 5 | 6 | 90.0 | USD |
| 8 | 1 | 1 | 225.0 | EUR |
| 9 | 7 | 2 | 350.0 | GBP |
| . | . | . | . | . |
| . | . | . | . | . |
| . | . | . | . | . |
| . | . | . | . | . |
+----+-------------+-------------+--------+----------+
这是我的自定义数据模型
public class CustomDataModel{
private Integer count; //record count
private double total; // its total amount for specified currency
private Currency currency; // it also could be String, it is doesn't matter
}
我的查询参数:merchant_id 和 customer_id.
我想得到结果 List<CustomDataModel>
例如:
[
{
"count":5,
"total":674.00,
"currency":"USD"
},
{
"count":3,
"total":561.00,
"currency":"EUR"
},
{
"count":4,
"total":715.00,
"currency":"GBP"
}
]
有没有可能得到这样的结果。如果是我该怎么办?
我想你想要 List<CustomDataModel>
按货币分组。如果是这样,则无需依赖 merchant_id
或 customer_id
。
您可以在您的存储库中添加以下 @Query
方法,如下所示:
@Query("select new com.demo.spring.domain.CustomDataModel(count(s), sum(s.amount), s.currency) from Sales s group by s.currency")
List<CustomDataModel> getGroupByCurrency();
只需确保将 Integer 转换为 Long,就像在 CustomDataModel
class 中的 - private Integer count;
到 private Long count;
并向其添加以下构造函数:
public CustomDataModel(Long count, double total, Currency currency) {
super();
this.count = count;
this.total = total;
this.currency = currency;
}
如果你想按商户和客户分组,你可以将上面的group by子句更新为...group by s.currency, s.merchant, s.customer