简体   繁体   中英

Spring boot doesnt read my data.sql file?

Hei guys, im pretty new to SpringBoot and i have 1 problem. When i start the app the spring read the schema.sql and make the tabels but the data.sql is not read, so i dont have no data on my db.

this is my 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

schema.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),
);

data.sql

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

It's just a simple app and query but it doesnt read it.

Add this property to app.prop spring.datasource.initialization-mode=always

Refer Link: 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

I hope this helps.

Either you put the file somewhere outside the resources (external file) or provided the location wrongly, - please check the logs

To confirm that, change below line
spring.datasource.data = classpath:db/data.sql
to:
spring.datasource.data = file:<full-path>db/data.sql

OR

in some spring-boot versions,
spring.sql.init.data-locations=classpath:db/data.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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