3

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?

1
  • You could use Spring JDBC to populate data in your application. Here you have example how to use it: docs.spring.io/spring-boot/docs/current/reference/html/… But better solution could be using Flyway or Liquibase. How to use this tools is also described in spring documentation. Commented Oct 27, 2016 at 15:23

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I had to name it import.sql in order to get it to work. Just throwing that out as a potential fix if anyone had problems.
@fudge, thanks so much for your comment. It helped me resolve the bug in my code by renaming data.sql to import.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.