Spring Boot - Loading Initial Data

-

schema.sql

  • src/main/resources
1
2
3
4
5
6
7
8
DROP TABLE IF EXISTS USERS;

CREATE TABLE USERS(
ID int NOT NULL AUTO_INCREMENT,
NAME varchar(100) NOT NULL,
STATUS int,
PRIMARY KEY (ID)
);

data.sql

1
2
3
4
5
6
7
8
INSERT INTO
users
VALUES
(1, 'Alex', 1),
(2, 'Bob', 1),
(3, 'John', 0),
(4, 'Harry', 0),
(5, 'Smith', 1);

note

  • Spring boot 2, database initialization only works for embedded databases (H2, HSQLDB, …) If you want to use it for other databases as well, you need to change the spring.datasource.initialization-mode property

    1
    spring.datasource.initialization-mode=always
  • If you’re using multiple database vendors, you can name your file data-h2.sql or data-mysql.sql depending on which database platform you want to use.
    To make that work, you’ll have to configure the spring.datasource.platform property though:

    1
    spring.datasource.platform=h2

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×