简体   繁体   中英

Populate MySql DB with initial data from .sql file in Spring boot

I'm using spring boot in combination with MySql DB. I have hibernate to create db tables, but I was wondering what is the easiest way to populate db with initial data such as 'users' by executing some queries from data.sql file? Also what dependencies should I add to pom.xml and properties to application-dev.yml for that matter?

Its very easy! What you need to do is create a sql file in the src/main/resources say data.sql file ; then the query you want say a insert query as :

 insert into admin(username,password,role)  values('admin','admin','role_admin');

Then add dependency :

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
     <version>1.2.4.RELEASE</version>
     </dependency>

And in application.properties file add the following code;

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     spring.datasource.url=jdbc:mysql://localhost:3306/exampleDB
     spring.datasource.username=root
     spring.datasource.password=root

For more info go through this : link

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