Spring Boot2 JPA 不会自动创建表的问题的各种解决方法
请你确认以下条款是否都做到了
方法一: JPA 实体类必须有 @Entity 注解
package com.haoshuashua.demo.jpa.entity; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; import java.math.BigDecimal; @Data @Entity // 这个注解很重要 @Table(name="t_assets") @NoArgsConstructor public class Asset { @Id //主键id @GeneratedValue(strategy= GenerationType.IDENTITY)//主键生成策略 @Column(name="id")//数据库字段名 private long id; // 用户编号 @Column(name="user_id") private long userId; }
方法二: Application 需要加上 @EnableAutoConfiguration 注解
package com.haoshuashua.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAutoConfiguration // 这个注解很重要 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
方法三: application.yml 里必须有 generate-ddl: true
spring: aop: auto: true profiles: active: ${SPRING_ENV:prd} servlet: multipart: enabled: true max-file-size: 100MB max-request-size: 120MB jpa: generate-ddl: true hibernate: hbm2ddl.auto: update show-sql: false datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver sql-script-encoding: UTF-8 druid: initial-size: 64 max-active: 128 min-idle: 10 max-wait: 30000 pool-prepared-statements: false test-on-borrow: false test-on-return: true test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 60000 filters: stat,slf4j web-stat-filter: enabled: true url-pattern: "/*" exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*,/static/*" stat-view-servlet: url-pattern: "/druid/*" login-username: admin login-password: 123123 enabled: true jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 default-property-inclusion: NON_NULL application: name: member-system
目前尚无回复