数据库里面的数据,日期是有的
查询语句
user类
thymeleaf语法
传输数据
显示的结果
两个错误改一下吧,第一个错误就是java映射数据库时间日期应该是Date,导入的包应该是java.util.Date
所以将时间类型LocalDateTime改成Date,改了时间类型还不行,你要这个时间设置一个输出格式,在时间字段属性上面添加注解,代码如下
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date gmtCreated;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date gmtModified;
并且在你的项目里面pom.xml引入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
第二个错就是,数据库和实体类属性如果不一致,就需要做映射,如果一致就不需要,很明显最后两个字段都没有做映射
就是在你的mapper.xml写数据库和实体类之间的映射,在select标签上面添加这个
<resultMap id="UserMap" type="User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="mobile" property="mobile"/>
<result column="gmt_created" property="gmtCreated"/>
<result column="gmt_modified" property="gmtModified"/>
</resultMap>
select标签里的resultType="User"改成resultMap="UserMap"即可
因为你的变量应该定义成Date
打断点看下数据库获得的信息,users里的user的时间获得没有。
映射的类型改成localdate试一下
sql中内容,返回字段中转换下,select id,username,password,gmt_created as gmtCreated ,gmt_modified as gmtModified from noise_user
resultType 中映射了字段名了吗