【问题标题】:Spring boot unable to create database schema and insert values on load at MYSQLSpring Boot 无法创建数据库模式并在 MYSQL 加载时插入值
【发布时间】:2023-01-15 06:38:24
【问题描述】:

当我启动它时,我无法让 spring boot 自动加载我的数据库模式。我正在使用 MySQL 作为外部数据库。请在下面找到代码

应用程序属性

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/carsdb
spring.datasource.username=root
spring.datasource.password=password

应用.java

package com.truckla.cars;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAutoConfiguration
@SpringBootApplication
public class CarsApplication {

    public static void main(String[] args) {
        SpringApplication.run(CarsApplication.class, args);
    }

}

模型实体

package com.truckla.cars.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "cars")
@SequenceGenerator(name="seq", initialValue=4, allocationSize=100)
public class Car {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    Integer id;
    String manufacturer;
    String model;
    Integer build;

    public Car() {
    }

    public Car(Integer id, String manufacturer, String model, Integer build) {
        this.id = id;
        this.manufacturer = manufacturer;
        this.model = model;
        this.build = build;
    }

    public Integer getId() {
        return id;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getModel() {
        return model;
    }

    public int getBuild() {
        return build;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setBuild(Integer build) {
        this.build = build;
    }
}

模式.sql

CREATE TABLE cars (
  id INT AUTO_INCREMENT  PRIMARY KEY,
  manufacturer VARCHAR(250) NOT NULL,
  model VARCHAR(250) NOT NULL,
  build YEAR DEFAULT NULL
);

数据.sql

INSERT INTO cars (manufacturer, model, build) VALUES
  ('Ford', 'Model T', 1927),
  ('Tesla', 'Model 3', 2017),
  ('Tesla', 'Cybertruck', 2019);

任何想法我做错了什么以及我应该改变什么才能让它发挥作用?提前致谢。

【问题讨论】:

    标签: java mysql spring-boot hibernate spring-data-jpa


    【解决方案1】:

    您是否尝试过在应用程序属性文件中使用以下属性?

    spring.jpa.defer-datasource-initialization=true

    spring.sql.init.mode=总是

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization.using-basic-sql-scripts

    【讨论】:

    • 没有什么改变..
    • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以在in the help center找到更多关于如何写出好的答案的信息。
    【解决方案2】:

    尝试使用以下属性运行。

    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto=create
    

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2019-08-08
      • 2017-11-11
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多