<select id="testSql" resultType="java.lang.Integer">
select
case month(o.create_time) when '1' then count(*) else 0 end as 1月,
case month(o.create_time) when '2' then count(*) else 0 end as 2月,
case month(o.create_time) when '3' then count(*) else 0 end as 3月,
case month(o.create_time) when '4' then count(*) else 0 end as 4月,
case month(o.create_time) when '5' then count(*) else 0 end as 5月,
case month(o.create_time) when '6' then count(*) else 0 end as 6月,
case month(o.create_time) when '7' then count(*) else 0 end as 7月,
case month(o.create_time) when '8' then count(*) else 0 end as 8月,
case month(o.create_time) when '9' then count(*) else 0 end as 9月,
case month(o.create_time) when '10' then count(*) else 0 end as 10月,
case month(o.create_time) when '11' then count(*) else 0 end as 11月,
case month(o.create_time) when '12' then count(*) else 0 end as 12月
from orders o
where date_format(o.create_time, '%Y') = YEAR (CURDATE())
</select>
查询到了,但是无法接收
@Test
void contextLoads() {
List<Integer> integers = ordersMapper.testSql();
System.out.println("===================================================");
System.out.println(integers.size()); // 1
System.out.println("===================================================");
}
resultType="java.lang.Integer"
、resultType="int"
resultType="integer"
、resultType="arraylist"
、resultType="list"
都没效果
最终还是选择了最笨的循环查询但是效率很低
<select id="getEveryMonthOrderNum" parameterType="string" resultType="int">
select case month (o.create_time) when #{m} then count(*) else 0 end
from orders o
where date_format(o.create_time, '%Y') = YEAR (CURDATE())
</select>
controller
@ApiOperation(value = "获得一年每月订单数")
@GetMapping("/getEveryMonthOrderNum")
public Result getEveryMonthOrderNum() {
String[] months = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
List list = new ArrayList<Integer>(12);
for (String month : months) {
list.add(ordersService.getEveryMonthOrderNum(month));
}
Map<String, Object> map = new HashMap<>(16);
map.put("everyMonthOrderNumList", list);
return Result.ok().data(map);
}
结果
{
"code": 200,
"msg": "成功",
"data": {
"everyMonthOrderNumList": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
14,
0,
0
]
}
}