public class OrderStatisticsVO {
//日期
private Date days;
//多少订单
private Integer amount;
}
查询封装
/**
* @Author: jsh
* @Describe: 往数据库查询使用 订单量统计的Query
* @Initialize the classpath: com.imooc.mall.model.query.OrderStatisticsQuery
* @System Username: song
* @project name: spring-boot-mall-shizhan
* @creation time: 2023-06-02 09:50
* @Versions 1.0
*/
public class OrderStatisticsQuery {
// @DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date startDate;
// @DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date endDate;
}
sql 如果需要话可以加字段 成正序或者倒叙
<resultMap id="listOrderStatisticsMap" type="com.imooc.mall.model.vo.OrderStatisticsVO">
<result column="days" jdbcType="TIMESTAMP" property="days"/>
<result column="amount" jdbcType="INTEGER" property="amount"/>
</resultMap>
<select id="selectOrderStatistics" resultMap="listOrderStatisticsMap">
select date_format(FROM_UNIXTIME(UNIX_TIMESTAMP(create_time)), '%Y-%m-%d')
as days,
count(*) as amount
from imooc_mall_order
<where>
<if test="query.startDate != null">
<![CDATA[ and create_time >= #{query.startDate} ]]>
</if>
<if test="query.endDate != null">
<![CDATA[ and create_time <= #{query.endDate} ]]>
</if>
</where>
group by days
order by days
</select>
时区问题
- 查看mysql的时区
select now()
- 查看springboot时区
@SpringBootApplication
public class MallApplication {
public static void main(String[] args) {
//输出时间
System.out.println(TimeZone.getDefault());
SpringApplication.run(MallApplication.class, args);
}
}
- json转换配置时区(配置(yaml)
spring:
jackson:
time-zone: Asia/Shanghai
date-format: yyyy-MM-dd HH:mm:ss
或者之接配置某字段
public class OrderStatisticsVO {
//日期
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date days;
//多少订单
private Integer amount;