LambdaQueryWrapper如何做sum操作
时间: 2024-01-08 17:21:32 浏览: 151
LambdaQueryWrapper可以通过使用select方法和lambda表达式来进行sum操作。下面是两个例子:
1. 使用LambdaQueryWrapper进行sum操作并获取结果:
```java
LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Order::getAmountTotal.sum().as("sumAll"));
Order ord = orderService.getOne(queryWrapper);
// 取值
BigDecimal sumAll = ord.getSumAll();
```
2. 使用LambdaQueryWrapper进行sum操作并返回结果:
```java
LambdaQueryWrapper<SbhPlatOrder> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(SbhPlatOrder::getAmt.sum().as("total"));
queryWrapper.between(SbhPlatOrder::getOrderEndTime, merPlatOrderRequest.getStartTime(), merPlatOrderRequest.getEndTime());
Map<String, Object> count = sbhPlatOrderManager.getMap(queryWrapper);
if (count == null) {
return Result.success(0L);
}
Double total = Double.valueOf(String.valueOf(count.get("total")));
return Result.success(total.longValue());
```
阅读全文