代码之家  ›  专栏  ›  技术社区  ›  Omar Ashraf

为什么我在创建待办事项实体时的日期为空?

  •  0
  • Omar Ashraf  · 技术社区  · 2 年前

    我的待办申请有什么问题?我希望用户能够添加todo,并在创建时将其保存在我的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添加待办事项

    
    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请求后,MySQL中的目标_日期变为空

    1 回复  |  直到 2 年前
        1
  •  0
  •   Yahor Barkouski    2 年前

    我想你可以用 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();
        }