方式一: 使用MySQL语句实现
当前时间函数
select now(); # 2022-05-26 12:45:34
select curdate(); # 2022-05-26
select curtime(); # 12:45:34
提取日期
select date(now()); # 2022-05-26
从日期减去指定的时间间隔
select date_sub(curdate(), interval 1 day); # 2022-05-25
近十天 每天的数据量
CREATETIME为时间字段
SELECT
count(*) AS totalcount,
CREATETIME
FROM
T_DOCUMENT_HEAD
WHERE
DATE_SUB(CURDATE(), INTERVAL 10 DAY) <= date(CREATETIME)
GROUP BY
date(CREATETIME);
方式二: 使用MyBatisPlus语句实现
Service层
Date date = new Date();
ArrayList<HeadCountVo> headCountVos = new ArrayList<>();
// 近十天的数据
for (int i = 0; i < 10; i++) {
// 某天的日期和数量
LambdaQueryWrapper<TDocumentHead> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.ge(TDocumentHead::getCreatetime, todayStr(date));
queryWrapper.le(TDocumentHead::getCreatetime, tomorrowStr(date));
HeadCountVo headCountVo = new HeadCountVo();
headCountVo.setDay(todayStr(date));
headCountVo.setNum(headService.count(queryWrapper));
headCountVos.add(headCountVo);
// 时间切换到上一天
date = yesterday(date);
}
return headCountVos ;
实体类
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class HeadCountVo {
/**
* 日期(天)
*/
@TableField(exist =false)
String day;
/**
* 数量
*/
@TableField(exist =false)
Integer num;
}