Dan*_*iel 2 java hibernate spring-data-jpa spring-boot
我正在尝试使用 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
Run Code Online (Sandbox Code Playgroud)
这是我的课:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是应用程序:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
@RestController @RequestMapping("/customer") public class CustomerController { @Autowired CustomerRepository customerRepository;
@RequestMapping("/findall")
@ResponseBody
public List<Customer> findAll() {
return customerRepository.findAll();
}
Run Code Online (Sandbox Code Playgroud)
}
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>
Run Code Online (Sandbox Code Playgroud)
尝试添加 @EntityScan
@EntityScan("<package with entities>")
@SpringBootApplication
public class Application { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13615 次 |
| 最近记录: |