The content discusses the importance of data persistence in Spring-managed applications, particularly through the use of databases. It lists the top seven enterprise databases for 2023, including MariaDB, Oracle Database, PostgreSQL, Microsoft SQL Server, MongoDB, Redis, and Elasticsearch. The text highlights the trend of companies opting for MariaDB and PostgreSQL for cost-effective solutions, especially in web applications.
Furthermore, it emphasizes the significance of a well-designed data access layer to avoid performance bottlenecks. The chapter outlines a series of topics that will be covered, including:
-
Comparison of traditional JDBC and Spring JDBC support.
-
Connection management differences between Connection and DataSource.
-
Techniques for retrieving data and mapping it to Java objects.
-
Methods for inserting, updating, and deleting data using Spring.
-
Testing JDBC code with in-memory databases and tools like Testcontainers.
-
Using Spring Boot JDBC for easy configuration of databases in different environments.
Overall, it serves as an introduction to working with SQL databases and JDBC in the context of Spring applications.
Sample Data Model for Example Code
The excerpt introduces a simple music database model used for examples in the chapter and subsequent chapters, featuring two tables: SINGER and ALBUM. The SINGER table holds singer details, while the ALBUM table contains information about albums released by each singer, establishing a one-to-many relationship between the two.
The chapter utilizes MariaDB, an open-source version of MySQL, highlighting its improved performance and suggesting the use of a Docker container for easy setup. Instructions for creating a music database schema and user are provided, along with SQL scripts for table creation and data population.
The text explains the necessity of a "translator" (database driver) for Java applications to communicate with the database, with options for using additional layers like Hibernate or Spring Data. This is exemplified by introducing POJOs for the singer and album tables, as well as a SingerDao
interface that encapsulates data access methods.
Additionally, the chapter encourages setting the logging level to DEBUG for detailed SQL execution output, aiding in troubleshooting. Sample code snippets illustrate the POJOs, the data access interface, and the logging configuration.