时区设置和 某段日期内订单汇总

文章提供了一个用于从数据库查询订单统计信息的Java类OrderStatisticsQuery,包含开始和结束日期属性,处理日期格式化和时区问题。SQL查询从imooc_mall_order表中按日期分组统计订单数量,支持时间范围筛选。同时,展示了如何在SpringBoot应用中配置Jackson以适应不同的日期格式和时区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

时区问题

  1. 查看mysql的时区
select   now()
  1. 查看springboot时区
@SpringBootApplication
public class MallApplication {

    public static void main(String[] args) {
        //输出时间
        System.out.println(TimeZone.getDefault());
        SpringApplication.run(MallApplication.class, args);
    }
}
  1. 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值