One of the most significant parts of a real-world application is where the data is stored. It affects the internal structure and performance of the whole application, and it may also simplify or complicate future development. Since there are many databases (such as MySQL, Postgres, MongoDB, Redis, and many others), how we interact with them becomes exceptionally important.
Providing a vast and diverse foundation for developing production-ready applications, the Spring framework couldn't ignore this crucial issue. Consequently, one of the most important Spring parts (or sub-frameworks), Spring Data, focuses entirely on data access and object mapping. It is designed to support a simple and unified way for data access. This approach is almost independent of the database type but still allows developers to take advantage of special features of the chosen database.
When using Spring Data, you don't usually need to consider low-level database details. Instead, you can concentrate on your object model. Spring Data automatically converts your class objects to the required database format.
Features
Spring Data provides a vast number of features to facilitate interactions with databases. Here is the list of the most essential of them.
Repository-oriented programming. Spring Data includes a concept named repository that hides all database-specific details and provides a high-level interface to interact with databases in an object-oriented manner. This approach allows developers to design a consistent data access level regardless of the database type.
Query generation based on repository method names. Database queries are generated automatically (e.g., SQL queries) based on the names of repository methods.
Database configuration. Spring Data provides a way to configure the interaction with a database using the configuration file or a programmatic setup.
Repository customization and native queries. When the query generation is insufficient, it is possible to customize your repositories to execute database-specific queries.
Integration with other parts of Spring. The Spring Data Framework is seamlessly integrated into the Spring ecosystem. For example, repositories can be used together with other Spring components. At the same time, Spring Data doesn't rely on Spring Web so it can be used in desktop and other Spring applications.
Audit support. Spring data provides the possibility to keep track of who created or changed objects stored in a database at what time.
Of course, this is not the complete list of features Spring Data provides, but these are the most important points for beginners.
Modules
Spring Data consists of several modules that support working with different types of databases. The number of Spring Data modules is constantly growing, and the full list looks impressive. Some modules are being developed under the guidance of Pivotal, while others are fully community-driven.
Here are some of those modules with brief descriptions.
Spring Data Commons is a core module that provides the most common functionality for interacting with databases, including repositories, transactions, audits, etc.
Spring Data JDBC provides a way to access relational databases using the Java Database Connectivity API (JDBC) with a repository-oriented approach.
Spring Data JPA is aimed at working with relational databases using the commonly used Java Persistence API (JPA) tool, which is a more advanced alternative to JDBC.
Spring Data MongoDB is designed to work with MongoDB using Spring Data repositories.
Spring Data Redis is designed to access Redis from Spring applications.
Spring Data REST allows exporting Spring Data repositories as REST services. Unlike others, this module relies on Spring Web and is intentionally designed for web applications.
There also are modules for storing your data in other databases, such as Apache Cassandra and Neo4J, and for interacting with more specialized storage systems like LDAP or Hazelcast. You don't have to include all these modules in your application. Depending on your database or storage, you will add one or more of them.
You don't need to add Spring Data Commons manually to your application because database-specific modules already include it.
Conclusion
Spring Data is a part of Spring that comes in handy when accessing a database. It provides a unified way to interact with different databases using repositories, which hide low-level database-specific queries. One of the distinctive features of the Spring Data framework is its high modularity. Next to the core module, there are multiple independent database-specific modules for all occasions (relational databases, MongoDB, Redis, and so on). In addition, most Spring Data modules do not rely on Spring Web. Consequently, you are not limited to using them in web applications.