Springboot中转换时间格式

在数据表中只查询时间这一列时,规定时间格式

在实体类中通过注解

public class DevTemp {
    
    
	private int id;
	private double temp;
	
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	private Date dDate;
}

在xml文件中

<resultMap id="TempMap"
		type="testmaven04_device.com.hzx.device.pojo.DevTemp">
		<id property="id" column="id" />
		<result property="temp" column="temp" />
		<result property="dDate" column="d_date" />
	</resultMap>

在yml配置文件中

在yml文件中配置时间格式,在实体类中不必加@JsonFormat注解

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    serialization:
      write-dates-as-timestamps: false

在xml文件,只查询时间这一列时,结果集类型直接使用date类型。

	<select id="queryTempDate" resultType="date">
		select d_date from t_temp;
	</select>

在这里插入图片描述

如果使用resultMap,结果集中仍会带有其他字段

	<resultMap id="DateMap"
	type="testmaven04_device.com.hzx.device.pojo.DevTemp">
		<result property="dDate" column="d_date"/>
	</resultMap>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41841482/article/details/114482911
今日推荐