Database Schema in Spring Boot
Spring Boot provides a convenient way to initialize and configure a relational database schema using SQL scripts. This feature allows developers to set up the database schema and populate it with initial data using schema.sql and data.sql files. These files should be placed in the classpath, and Spring Boot will automatically execute them during application startup.
Configuring the Database Schema
In a typical Spring Boot application, you can define the database schema using a schema.sql file. This file contains the SQL statements necessary to create the database tables and define their structure. For instance, consider a sample application that manages Course details. The Course is the primary business domain object, and the database schema is configured to create a COURSES table.
Example: schema.sql
The following SQL script creates a COURSES table with columns for id, name, category, rating, and description. The id column is defined as the primary key.
Listing 3.9 Database schema.sql configuration
CREATE TABLE COURSES
(
id int(15) NOT NULL,
name varchar(100) NOT NULL,
category varchar(20) NOT NULL,
rating int(1) NOT NULL,
description varchar(1000) NOT NULL,
PRIMARY KEY (id)
);
Populating Initial Data
Once the database schema is set up, you can populate it with initial data using a data.sql file. This file contains Data Manipulation Language (DML) statements, such as INSERT, to add records to the tables created by the schema.sql script.
Example: data.sql
The data.sql file includes INSERT statements to add sample courses to the COURSES table. This initial data can be used for testing or demonstration purposes.
Validating Database Schema Initialization
To ensure that Spring Boot correctly initializes the database schema, you can write a unit test. This test can verify the number of records in the COURSES table, confirming that the data.sql script has been executed successfully.
Example: Unit Test
The following unit test uses basic JDBC code to count the number of courses in the COURSES table. It asserts that there are five courses, as defined in the data.sql file.
Listing 3.11 Unit test to validate database schema initialization
package com.manning.sbip.ch03;
// Import Statements are excluded as a matter of readability
@SpringBootTest
class CourseTrackerSpringBootApplicationTests {
@Autowired
private DataSource dataSource;
@Test
public void whenCountAllCoursesThenExpectFiveCourses()
throws SQLException {
ResultSet rs = null;
int noOfCourses = 0;
try(PreparedStatement ps =
dataSource.getConnection().prepareStatement("SELECT COUNT(1) FROM
COURSES")) {
rs = ps.executeQuery();
while(rs.next()) {
noOfCourses = rs.getInt(1);
}
assertThat(noOfCourses).isEqualTo(5L);
}
finally {
if(rs != null) {
rs.close();
}
}
}
}
In this test, the DataSource is autowired, and a SQL query is executed to count the courses. The test asserts that the number of courses is five, matching the number of INSERT statements in the data.sql file.
FAQ (Frequently asked questions)
How can you initialize a database schema in a Spring Boot application?
Where should you place the schema.sql and data.sql scripts in a Spring Boot application?
What does Spring Boot do with schema.sql and data.sql scripts?