This is a simple Spring Boot project that demonstrates how to initialize a database using multiple, organized SQL script files at startup.
Instead of relying on the default schema.sql and data.sql files, this project uses a custom DatabaseInitializationService to load scripts from specific directories (db/migration/schema/ and db/migration/data/). This allows for better organization of your database scripts.
- Java 21
- Spring Boot
- Spring Data JPA: For database interaction
- Spring Web: For creating REST endpoints
- MySQL: The relational database
- Lombok: To reduce boilerplate code
- You must have Java 21 (JDK) installed.
- You must have a MySQL Server running on your computer.
-
Create the Database: Before running the application, you must create the database that Spring Boot will connect to. Run this command in your MySQL client (like MySQL Workbench or the command line):
CREATE DATABASE example;
-
Edit
application.properties: Open thesrc/main/resources/application.propertiesfile. You must update the username and password to match your local MySQL credentials.spring.datasource.url=jdbc:mysql://localhost:3306/example spring.datasource.username=root spring.datasource.password=Aman
The easiest way to run the project is from your IDE (like IntelliJ IDEA):
- Find the
SpringBootWithMultipleSqlImportFilesApplication.javafile. - Right-click on it and select "Run".
The application will start on http://localhost:8080.
Once the application is running, the database will be automatically initialized with data from the SQL files. You can view the data by visiting these URLs in your web browser:
-
Get All Users:
http://localhost:8080/api/users -
Get All Products:
http://localhost:8080/api/products
-
service/DatabaseInitializationService.javaThis is the most important file. The@PostConstructannotation tells Spring to run theinitializeDatabase()method at startup, which executes all scripts in the schema and data folders. -
application.propertiesNote thatspring.jpa.hibernate.ddl-auto=noneis set. This is important! It tells Hibernate not to create tables, allowing our custom scripts to take full control. -
src/main/resources/db/migration/schema/This directory holds all theCREATE TABLEscripts (schema1.sql,schema2.sql). -
src/main/resources/db/migration/data/This directory holds all theINSERT INTOscripts (data1.sql,data2.sql).
- Aman Manwatkar