SpringBoot系列之MybatisPlus实现分组查询
我之前博主曾记写过一篇介绍SpringBoot2.0项目怎么集成MybatisPlus的教程,不过之前的博客只是介绍了怎么集成,并没有做详细的描述各种业务场景,本篇博客是对之前博客的补充,介绍在mybatisPlus项目中怎么使用分组查询,仅供参考借鉴
项目环境
- 开发环境
- JDK 1.8
- SpringBoot2.2.1
- Maven 3.2+
- Mysql5.7.36
- 开发工具
- IntelliJ IDEA
- smartGit
实践例子
具体的搭建过程,请参考博主之前博客,为了方便拓展和不影响其它业务,可以基于基本的UserDO
类,加上我们分组需要的字段,如下所示:
package com.example.mybatisplus.model;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
@TableName("user")
@Data
public class UserCountDO extends UserDO implements Serializable {
private static final long serialVersionUID = 3319012504813563043L;
@TableField(value = "count(*)", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
private Long count;
@TableField(value = "date_format(create_time, '%Y年%m月%d日')", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
private String chineseDateFormat;
}
在重新定义一个Mapper接口
package com.example.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus.model.UserCountDO;
public interface UserCountMapper extends BaseMapper<UserCountDO> {
}
测试类来实现
package com.example.mybatisplus;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.mybatisplus.mapper.UserCountMapper;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.model.UserCountDO;
import com.example.mybatisplus.model.UserDO;
import com.example.mybatisplus.model.vo.UserCountVo;
import net.minidev.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
import java.util.stream.Collectors;
@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserCountMapper userCountMapper;
@Test
void testGroupBy() {
LambdaQueryWrapper<UserCountDO> selectWrapper = Wrappers.lambdaQuery();
selectWrapper.select(UserCountDO::getChineseDateFormat, UserCountDO::getCount);
selectWrapper.groupBy(UserCountDO::getChineseDateFormat);
List<Map<String, Object>> userMapList = Optional.ofNullable(userCountMapper.selectMaps(selectWrapper)).orElse(Collections.emptyList());
List<UserCountVo> resultList = new ArrayList<>();
for (Map<String, Object> e : userMapList) {
String chineseDateFormat = Convert.toStr(e.get("chineseDateFormat"));
Long count = Optional.ofNullable(Convert.toLong(e.get("count"))).orElse(0L);
UserCountVo userCountVo = UserCountVo.builder()
.value(chineseDateFormat)
.num(count)
.build();
resultList.add(userCountVo);
}
Date startTime = DateUtil.date(1705248000000L);
Date endTime = DateUtil.date(1705852800000L);
List<DateTime> dateList = Optional.ofNullable(DateUtil.rangeToList(startTime, endTime, DateField.DAY_OF_YEAR)).orElse(Collections.emptyList());
for (DateTime dateTime : dateList) {
String format = DateUtil.format(dateTime, "YYYY年MM月dd日");
if (resultList.contains(format)) {
continue;
}
UserCountVo messageCountDto = UserCountVo.builder()
.value(format)
.num(0L)
.build();
resultList.add(messageCountDto);
}
List<UserCountVo> sortedResultList = resultList.stream().sorted(Comparator.comparing(UserCountVo::getValue, Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonPrettyStr(sortedResultList));
}
}
控制台日志打印:这些数据可以用于Echarts的图标显示