解决Spring Boot不能通过实体类生成数据表问题
1、在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、properties或者yml文件添加配置
properties配置如下:
server.port=8080
characterEncoding=utf8
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
如果你是写的yml文件,参照如下配置:
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ims?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
3、实体类添加注解@Entity
@Entity
public class Goods {
/**
* 商品ID,主键
*/
@Id
@Column(nullable = false,unique = true)
private String goodsId;
/**
* 商品名
*/
private String name;
/**
* 介绍
*/
private String intro;
/**
* 价格
*/
private BigDecimal price;
/**
* 数量
*/
private Integer num;
}
本文介绍了如何解决Spring Boot应用无法通过实体类自动创建数据表的问题,主要包括在pom.xml中添加相关依赖,配置properties或yml文件,以及在实体类上使用@Entity注解。
721

被折叠的 条评论
为什么被折叠?



