Spring Boot无法在MYSQL上创建数据库模式并在加载时插入值

Spring boot unable to create database schema and insert values on load at MYSQL

提问人:Loopinfility 提问时间:11/5/2022 最后编辑:Loopinfility 更新时间:1/15/2023 访问量:365

问:

我无法让 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-data-jpa

评论


答:

0赞 Abhishek 11/5/2022 #1

您是否尝试过在application-properties文件中使用以下属性?

spring.jpa.defer-datasource-initialization=true

spring.sql.init.mode=always

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

评论

0赞 Loopinfility 11/5/2022
不,什么都没有改变。.
0赞 Community 11/8/2022
您的答案可以通过其他支持信息进行改进。请编辑以添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 Shamsher 11/5/2022 #2

尝试使用以下属性运行。

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