Mysql
 sql >> база данни >  >> RDS >> Mysql

Springboot + MySQL:не може да чете никакъв тип дата от (ДАТА, ДАТА, ВРЕМЕ, TIMESTAMP) MySQL

Направих тестова настройка с вашите класове и трябва да работи :) Надявам се това да е полезно.

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL Невалидна стойност по подразбиране за времеви печат, когато не е дадена стойност по подразбиране.

  2. Грешка 1046 Няма избрана база данни, как да се разреши?

  3. Как да генерирате скрипт от диаграма в MySQL Workbench

  4. MySQL - Как да изберете данни по дължина на низа

  5. Как да направя пагинация с mybatis?