本文介绍了Springboot + MySQL:无法从(DATE,DATETIME,TIMESTAMP)MySQL中读取任何日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 
与mysql:DATE
的java.sql.Date与mysql:DATETIME
的mysql:DATETIME
与mysql:DATE的java.util.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

我的模型:

 公共课评论{

私人int id;
私有字符串内容;
private int userId;
/ *我也在MYSQL中为DATETIME尝试了java.util.Date,它的结果相同* /
private Timestamp createDate;
private int entityId;
private int entityType;
私人int状态;
...
}

每次我调试应用程序时, createDate总是返回一个空。

解决方案

我对您的类进行了测试设置,它应该可以正常工作:)我希望



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

 软件包stackoverflow; 

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
公共类评论{

@Id
@GeneratedValue
private int id;
私有字符串内容;
private int userId;
/ *我也在MYSQL中为DATETIME尝试了java.util.Date,它的结果相同* /
private Timestamp createDate;
private int entityId;
private int entityType;
私人int状态;
public int getId(){
返回ID;
}
public void setId(int id){
this.id = id;
}
public String getContent(){
返回内容;
}
public void setContent(String content){
this.content = content;
}
public int getUserId(){
return userId;
}
public void setUserId(int userId){
this.userId = userId;
}
公共时间戳记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(intEntityType){
this.entityType = entityType;
}
public int getStatus(){
返回状态;
}
public void setStatus(int status){
this.status = status;
}


}

CommentRepo.java

 软件包stackoverflow; 

导入org.springframework.data.repository.CrudRepository;

公共接口CommentRepo扩展了CrudRepository< Comment,Integer> {

}

CommentLoader.java

 软件包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
公共类CommentLoader实现ApplicationListener< ContextRefreshedEvent> {

@Autowired
CommentRepo commentRepo;

@Override
public void onApplicationEvent(ContextRefreshedEvent event){
Calendar cal = Calendar.getInstance();
Comment comment = new Comment();
comment.setContent( some chotent);
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());
}
}

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

这篇关于Springboot + MySQL:无法从(DATE,DATETIME,TIMESTAMP)MySQL中读取任何日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:23