简体   繁体   English

使用Spring引导中的.sql文件中的初始数据填充MySql DB

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

I'm using spring boot in combination with MySql DB. 我将Spring Boot与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? 我有hibernate来创建db表,但是我想知道通过从data.sql文件执行一些查询来填充db和初始数据(例如'users')的最简单方法是什么? Also what dependencies should I add to pom.xml and properties to application-dev.yml for that matter? 此外,我应该将什么依赖项添加到pom.xml和属性到application-dev.yml?

Its very easy! 很容易! What you need to do is create a sql file in the src/main/resources say data.sql file ; 你需要做的是在src / main / resources中创建一个sql文件,例如data.sql文件; 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; 在application.properties文件中添加以下代码;

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 欲了解更多信息,请访问: 链接

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

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