由于 LambdaQueryWrapper 并不支持使用sum等求和的聚合函数。所以只能退而求其次使用 QueryWrapper 来进行构建查询语句。
简单的例子如下:
//求某人的成绩和。
QueryWrapper<People> queryWrapper = new QueryWrapper<>();
queryWrapper.select("IFNULL(sum(score),0) as totalScore")
.eq("user_name", userName);
Map<String, Object> map = this.getMap(queryWrapper);
BigDecimal sumCount = (BigDecimal) map.get("totalScore");
上面的语句构建成可执行的MySQL语句即为:
--如果为空则默认返回默认值0
SELECT IFNULL(sum(score),0) AS total FROM people WHERE user_name = ?
PS:sum聚合函数求出来的值类型为 BigDecimal 类型。