为什么我在创建 Todo 实体时得到的日期为空?
Why am I getting null for the date when I create a Todo entity?
我的待办事项应用程序有什么问题?我希望用户能够添加待办事项并在创建时将其保存在我的 MySQL 数据库中,但我不知道我做错了什么。
我刚开始学习 Springboot,如果有任何建议或建议,我将不胜感激。
待办实体:
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.util.Date;
@Entity(name = "Todo")
@NoArgsConstructor
@Table(name = "todos")
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name="description")
private String description;
@Column(name="target_date")
@CreationTimestamp
private Date targetDate;
public Todo(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getTargetDate() {
return targetDate;
}
public void setTargetDate(Date targetDate) {
this.targetDate = targetDate;
}
@Override
public String toString() {
return "Todo{" +
"id=" + id +
", description='" + description + '\'' +
", targetDate=" + targetDate +
'}';
}
}
使用 Spring 数据 JPA 添加 Todo
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;
@Repository
@Component
public interface TodoRepository extends JpaRepository<Todo, Integer> {
@Modifying
@Query(value = "INSERT INTO todos (description) VALUES (:description)", nativeQuery=true)
@Transactional
void addTodo(@Param("description") String description);
}
TodoController
@RestController
@RequestMapping(value = "/api/v1/todos")
@AllArgsConstructor
public class TodoController {
@Autowired
private ITodoService todoService;
@PostMapping(value = "/add-todo")
public String addTodo(@RequestParam String description) {
Todo todo = new Todo();
todo.setDescription(description);
todoService.addTodo(todo);
return todo.toString();
}
收到 post 请求后,target_date 在 MySQL
中变为 NULL
我假设你可以使用 persist()
:
来解决它
@Autowired EntityManager entityManager;
@PostMapping(value = "/add-todo")
public String addTodo(@RequestParam String description) {
Todo todo = new Todo();
todo.setDescription(description);
entityManager.persist(todo);
return todo.toString();
}
我的待办事项应用程序有什么问题?我希望用户能够添加待办事项并在创建时将其保存在我的 MySQL 数据库中,但我不知道我做错了什么。
我刚开始学习 Springboot,如果有任何建议或建议,我将不胜感激。
待办实体:
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.util.Date;
@Entity(name = "Todo")
@NoArgsConstructor
@Table(name = "todos")
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name="description")
private String description;
@Column(name="target_date")
@CreationTimestamp
private Date targetDate;
public Todo(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getTargetDate() {
return targetDate;
}
public void setTargetDate(Date targetDate) {
this.targetDate = targetDate;
}
@Override
public String toString() {
return "Todo{" +
"id=" + id +
", description='" + description + '\'' +
", targetDate=" + targetDate +
'}';
}
}
使用 Spring 数据 JPA 添加 Todo
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;
@Repository
@Component
public interface TodoRepository extends JpaRepository<Todo, Integer> {
@Modifying
@Query(value = "INSERT INTO todos (description) VALUES (:description)", nativeQuery=true)
@Transactional
void addTodo(@Param("description") String description);
}
TodoController
@RestController
@RequestMapping(value = "/api/v1/todos")
@AllArgsConstructor
public class TodoController {
@Autowired
private ITodoService todoService;
@PostMapping(value = "/add-todo")
public String addTodo(@RequestParam String description) {
Todo todo = new Todo();
todo.setDescription(description);
todoService.addTodo(todo);
return todo.toString();
}
收到 post 请求后,target_date 在 MySQL
中变为 NULL我假设你可以使用 persist()
:
@Autowired EntityManager entityManager;
@PostMapping(value = "/add-todo")
public String addTodo(@RequestParam String description) {
Todo todo = new Todo();
todo.setDescription(description);
entityManager.persist(todo);
return todo.toString();
}