Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springboot&x2B;MySQL:无法从(日期、日期时间、时间戳)MySQL读取任何日期类型_Java_Mysql_Date_Datetime_Spring Boot - Fatal编程技术网

Java Springboot&x2B;MySQL:无法从(日期、日期时间、时间戳)MySQL读取任何日期类型

Java Springboot&x2B;MySQL:无法从(日期、日期时间、时间戳)MySQL读取任何日期类型,java,mysql,date,datetime,spring-boot,Java,Mysql,Date,Datetime,Spring Boot,我正在使用Springboot和MYSQL。我的应用程序可以将日期类型(java.util.date、java.sql.Timestamp)写入mysql,类型为(date、DATETIME、Timestamp)。然而,无论我在Java和MySQL中使用什么日期类型,Spring都无法从MySQL中读取日期。有人知道如何解决这个问题吗?先谢谢你。 1) 它适用于其他数据类型,如INT、TEXT、VARCHAR等。一切正常。 2) 我检查了MYSQL中存储的日期,它们都是正确的格式。 3) 我尝试

我正在使用Springboot和MYSQL。我的应用程序可以将日期类型(java.util.date、java.sql.Timestamp)写入mysql,类型为(date、DATETIME、Timestamp)。然而,无论我在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”。

我用你的类做了一个测试设置,它应该可以工作:)我希望这是有用的

应用程序属性

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> {

}
包堆栈溢出;
导入org.springframework.data.repository.crudepository;
公共接口CommentRepo扩展了Crudepository{
}
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());
    }       
}
包堆栈溢出;
导入java.sql.Timestamp;
导入java.util.Calendar;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.ApplicationListener;
导入org.springframework.context.event.ContextRefreshedEvent;
导入org.springframework.stereotype.Component;
@组成部分
公共类CommentLoader实现ApplicationListener{
@自动连线
CommentRepo CommentRepo;
@凌驾
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
Calendar cal=Calendar.getInstance();
注释=新注释();
comment.setContent(“某些内容”);
setCreateDate(新的时间戳(cal.getTimeInMillis());
评论:setStatus(1);
Comment savedComment=commentRepo.save(Comment);
Comment comment2=commentRepo.findOne(savedComment.getId());
System.out.println(“数据:+comment2.getCreateDate());
}       
}


几天后,我终于找到了问题所在。我在MYSQL的注释表中使用了名称“created_date”,但在注释模型中使用了名称“createDate”。这就是为什么我只能向数据库写入数据而无法从中读取日期的原因。

这不是对您问题的回答,但您应该更喜欢现代Java日期和时间类型:
LocalDate
以及至少一种
LocalDateTime
Instant
。不过,我还没有在弹簧靴上使用它们的经验,但这肯定是可能的。谢谢,伙计,我终于找到了问题所在。这只是命名的问题…就是这样。。。