问题:当我通过bash控制台发出发布请求时,就会创建一个新表。其余查询转到新表。比他不喜欢那些可用的数据库。据我了解-他们只是不知道,但是我不知道如何正确地进行指导。尽管所有变量也都被命名。
发现由于Message类中的Entity注释而创建的问题。请告诉我如何将其添加到现有表中,并尝试将@Table(name = "ApiTable")
添加到现有表中,并生成一个新的api_table。也不太了解为接受json发布请求需要添加/更改的内容。] >
应用程序
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication @EnableJpaRepositories("com.example.api") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
MainController
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(path="/demo") / public class MainController { @Autowired private UserRepository TestApi; @PostMapping(path="/add") public @ResponseBody String addNewUser (@RequestParam String name , @RequestParam String email) { Message n = new Message(); n.setName(name); n.setEmail(email); TestApi.save(n); return "Saved"; } @GetMapping(path="/all") public @ResponseBody Iterable<Message> getAllUsers() { return TestApi.findAll(); } }
消息
import javax.persistence.*; @Entity public class Message { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; private String name; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
UserRepository
import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository<Message, Integer> { }
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost/Test?useUnicode=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
问题:当我通过bash控制台发出发布请求时,就会创建一个新表。其余查询转到新表。比他不喜欢那些可用的数据库。当我...