繁体   English   中英

Spring 启动不读取我的数据。sql 文件?

[英]Spring boot doesnt read my data.sql file?

嘿伙计们,我对 SpringBoot 很陌生,我有 1 个问题。 当我启动应用程序时,spring 读取架构。sql 并制作表格,但 data.sql 未读取,所以我的数据库中没有数据。

这是我的 app.prop

spring.datasource.url = jdbc:mysql://localhost:3306/garagesaledb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.sql.init.mode=always 
spring.datasource.schema = classpath:db/schema.sql
spring.datasource.data = classpath:db/data.sql
spring.jpa.hibernate.ddl-auto=update
spring.liquibase.drop-first=true
spring.jpa.defer-datasource-initialization=true

架构.sql

create table asset
(
    id bigint not null auto_increment,
    category varchar(255),
    price decimal(10,2),
    quantity integer,
    purchaseOrder_id bigint,
    primary key(id),
);

数据.sql

INSERT INTO garagesaledb.asset(category, price, quantity,purchaseOrder_id) VALUES
('MOUSE', 10.0, 1, null);

这只是一个简单的应用程序和查询,但它不会读取它。

将此属性添加到 app.prop spring.datasource.initialization-mode=always

参考链接: https://docs.spring.io/spring-boot/docs/2.1.0.M1/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc

Spring Boot automatically creates the schema of an embedded DataSource. This behaviour can be customized by using the spring.datasource.initialization-mode property. For instance, if you want to always initialize the DataSource regardless of its type:
    spring.datasource.initialization-mode=always

我希望这有帮助。

您将文件放在resources之外的某个位置(外部文件)或错误地提供了位置,请检查日志

要确认这一点,请更改以下行
spring.datasource.data = classpath:db/data.sql
至:
spring.datasource.data = file:<full-path>db/data.sql

或者

在某些 spring-boot 版本中,
spring.sql.init.data-locations=classpath:db/data.sql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM