jpa mysql timestamp,Springboot + MySQL:无法从(DATE,DATETIME,TIMESTAMP)MySQL中读取任何日期类型...

I am using Springboot and MYSQL. My app can write date type (java.util.Date, java.sql.Timestamp) into mysql with type (DATE, DATETIME, TIMESTAMP). However, no matter what date type I used in Java and MySQL, Spring just cannot read the date from MySQL.Does anyone has any clue of how to solve the problem? Thanks a lot in advance.

1) it works for other data types, INT, TEXT, VARCHAR etc. everything works fine.

2) I checked the date stored in MYSQL, they are all in the right format.

3) The date type combination I tried are:

java.sql.Date with mysql:DATETIME

java.sql.Date with mysql:DATE

java.util.Date with mysql:DATETIME

java.util.Date with mysql:DATE

java.sql.Timestamp mysql:TIMESTAMP

They are all the same, I can successfully write into mysql, but cannot read the date information from the database.

My properties file:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/forum_demo

?useUnicode=true&characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=****

mybatis.config-location=classpath:mybatis-config.xml

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

My Model:

public class Comment {

private int id;

private String content;

private int userId;

/* I tried java.util.Date here as well for DATETIME in MYSQL, it is the same result*/

private Timestamp createDate;

private int entityId;

private int entityType;

private int status;

...

}

Everytime I debug my application, the getter of createDate always return a "null".

解决方案

I made a test setup with your classes and it should be working :) I hope this is helpfull.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/forum_demo?useUnicode=true&characterEncoding=utf8

spring.datasource.username=testuser

spring.datasource.password=testuser

#mybatis.config-location=classpath:mybatis-config.xml

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql = true

Comment.java

package stackoverflow;

import java.sql.Timestamp;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

@Entity

public class Comment {

@Id

@GeneratedValue

private int id;

private String content;

private int userId;

/* I tried java.util.Date here as well for DATETIME in MYSQL, it is the same result*/

private Timestamp createDate;

private int entityId;

private int entityType;

private int status;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

public Timestamp getCreateDate() {

return createDate;

}

public void setCreateDate(Timestamp createDate) {

this.createDate = createDate;

}

public int getEntityId() {

return entityId;

}

public void setEntityId(int entityId) {

this.entityId = entityId;

}

public int getEntityType() {

return entityType;

}

public void setEntityType(int entityType) {

this.entityType = entityType;

}

public int getStatus() {

return status;

}

public void setStatus(int status) {

this.status = status;

}

}

CommentRepo.java

package stackoverflow;

import org.springframework.data.repository.CrudRepository;

public interface CommentRepo extends CrudRepository {

}

CommentLoader.java

package stackoverflow;

import java.sql.Timestamp;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationListener;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Component;

@Component

public class CommentLoader implements ApplicationListener {

@Autowired

CommentRepo commentRepo;

@Override

public void onApplicationEvent(ContextRefreshedEvent event) {

Calendar cal = Calendar.getInstance();

Comment comment = new Comment();

comment.setContent("some conetent");

comment.setCreateDate(new Timestamp(cal.getTimeInMillis()));

comment.setStatus(1);

Comment savedComment = commentRepo.save(comment);

Comment comment2 = commentRepo.findOne(savedComment.getId());

System.out.println("Datum : " + comment2.getCreateDate());

}

}

IMaPK.jpg

8YBxf.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值