我尝试使用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);
}
}
这是控制器:
@RestController @RequestMapping(" /客户&#34) 公共类CustomerController { @Autowired CustomerRepository customerRepository;
@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>
答案 0 :(得分:2)
尝试添加
@EntityScan("<package with entities>")
答案 1 :(得分:1)
根据目前的信息,我有一些建议/问题可能对你有所帮助:
我在你的代码中看不到@Repository定义,例如
@Repository 公共接口CustomerRepository扩展了JpaRepository {}
答案 2 :(得分:1)
在你的mysql上执行以下命令:
的MySQL&GT; create database db_example; - 创建新数据库
的MySQL&GT;创建用户&#39; springuser&#39; @&#39; localhost&#39;由ThePassword&#39;标识; - 创建用户
的MySQL&GT;将所有内容授予db_example。*以及#spring;&#39; @&#39; localhost&#39 ;; - 在新创建的数据库上为新用户提供所有权限
并添加到application.xml
:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
OR
您可以浏览this link