gpt4 book ai didi

java - 无法用jpa+spring boot创建表

转载 作者:行者123 更新时间:2023-11-29 08:31:33 25 4
gpt4 key购买 nike

我正在尝试使用 JPA 生成表格。但我无法创建它。代码没有错误,但是好像是配置错误。但我找不到它,我尝试了很多配置但没有任何反应。非常感谢。

这是application.property:

spring.datasource.url=jdbc:mysql://localhost:3306/springapp
spring.datasource.username= root
spring.datasource.password= me
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create

这是我的课:

@Entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String phone;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Customer(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public Customer() {
super();
}
}

这是应用程序:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

这是 Controller :

@RestController@RequestMapping("/客户")公共(public)类 CustomerController { @Autowired 客户资料库客户资料库;

@RequestMapping("/findall")
@ResponseBody
public List<Customer> findAll() {
return customerRepository.findAll();
}

Pom.xml

    <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>GStock-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>GStock-3</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

最佳答案

尝试添加@EntityScan

@EntityScan("<package with entities>")
@SpringBootApplication
public class Application { ... }

关于java - 无法用jpa+spring boot创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com