【问题标题】:Springboot + MySQL: cannot read any date type from (DATE, DATETIME, TIMESTAMP) MySQLSpringboot + MySQL:无法从(DATE,DATETIME,TIMESTAMP)MySQL中读取任何日期类型
【发布时间】:2018-06-30 17:28:38
【问题描述】:

我正在使用 Springboot 和 MYSQL。我的应用程序可以将日期类型(java.util.Date,java.sql.Timestamp)写入类型为(DATE,DATETIME,TIMESTAMP)的mysql。但是,无论我在 Java 和 MySQL 中使用什么日期类型,Spring 都无法从 MySQL 中读取日期。有没有人知道如何解决这个问题?提前非常感谢。 1) 它适用于其他数据类型,INT、TEXT、VARCHAR 等。一切正常。 2)我检查了存储在MYSQL中的日期,它们的格式都是正确的。 3)我尝试的日期类型组合是:

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

都是一样的,我可以成功写入mysql,但是不能从数据库中读取日期信息。

我的属性文件:

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

我的模特:

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;
...
}

每次我调试我的应用程序时,createDate 的 getter 总是返回一个“null”。

【问题讨论】:

  • 这不是您问题的答案,但您应该更喜欢现代 Java 日期和时间类型:LocalDate 以及至少 LocalDateTimeInstant 之一。不过,我还没有在 Spring Boot 中使用它们的经验,但它肯定是可能的。

标签: java mysql date datetime spring-boot


【解决方案1】:

几天后,我终于发现出了什么问题。我在 MYSQL 的评论表中使用了名称“created_date”,但在评论模型中使用了名称“createDate”。这就是为什么我只能写入数据库而不能从中读取日期的原因。

【讨论】:

    【解决方案2】:

    我用你的课程做了一个测试设置,它应该可以工作:) 我希望这会有所帮助。

    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<Comment, Integer> {
    
    }
    

    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<ContextRefreshedEvent> {
    
        @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());
        }       
    }
    

    【讨论】:

    • 谢谢,伙计,我终于知道出了什么问题。这只是关于命名......就是这样......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2017-01-25
    • 2019-12-09
    • 2012-03-18
    • 2016-08-26
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多