【问题标题】:I used Spring Data JPA to create a table in MySQL, but the creation failed我使用Spring Data JPA在MySQL中创建表,但是创建失败
【发布时间】:2019-08-23 07:53:38
【问题描述】:

我使用 Spring Data JPA 在 MySQL 中创建表,但是创建失败。在我的实体类中找不到错误,我认为配置没有错。

我修改了属性中的一些jdbc连接语句,以及实体类上的注解,但是没有用。

@Data
@Entity
@Table
public class User {
    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;

    @Column(unique = true)
    private String userName;

    @Column
    private String password;

}

属性文件如下:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root 
spring.datasource.password=localhost # Spring Data Jpa 
spring.jpa.hibernate.ddl-auto=create 
spring.jpa.show-sql=true

【问题讨论】:

  • 控制台是否有任何错误或异常?你能提供连接细节吗?
  • # MySQL spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf8&useSSL =false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=localhost # Spring Data Jpa spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true
  • 这是我的属性文件
  • varchar 如何自动递增?我建议您将其更改为例如integer
  • @SudhirOjha 破折号分隔的属性对于 Spring Boot 是正确的,是骆驼大小写变体的替代方案。

标签: java spring-boot jdbc spring-data-jpa


【解决方案1】:

作为最佳实践,id 应始终为 Long 类型。请更改它,它应该可以工作。

【讨论】:

    【解决方案2】:

    我不知道 String 是如何自动递增的,最好改为 Longint。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 2020-01-28
      • 1970-01-01
      • 2020-05-21
      • 2019-02-03
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多