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?
-
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.krynio– krynio2016-10-27 15:23:50 +00:00Commented Oct 27, 2016 at 15:23
Add a comment
|
1 Answer
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