This video provides an introduction to the Spring Data platform with descriptions of the major projects in this space.
- [Lecturer] Welcome to Spring Data. This is the project that is by far my most favorite within the Spring framework and the reason is, is no single project has reduced more boilerplate code for me than the Spring Data project. So let's talk a little bit about what is Spring Data. Spring Data provides a common set of interfaces for a variety of datasources and technologies. Now, what that really means is not only do I have the traditional RDBMS datasources and all of its various flavors, but I also have all of the NoSQL database options, and Spring Data's interfaces connect to both in almost seamless integration.
Spring Data also provides a common method naming convention that allows me to swap those datasources behind the scenes almost effortlessly. Spring Data also provides a common interface for the repository pattern and all of the data mapping abstractions that we normally have to deal with. Some of the basic benefits of Spring Data, as I alluded to a few seconds ago, has to do with the reduction of boilerplate code and I'd like to take a quick look at what that looks like.
What you have in front of you now is how we would traditionally do a JDBC connection in Java. If you take a look here, we have to deal with the connection, we create that connection, we build a statement, we execute a query against that statement to get a result set, then we have to iterate over our results set to create our objects, also we can return that object and all of that mapping has to be done manually for each and every object that we create, and all of the connections set up and tear down has to be done for each database call that we make.
Now I'd like to take a look at the equivalent amount of code looks like in Spring Data. Now, to be fair, there is a little bit of setup that is going on behind the scenes, and we're going to get into what that looks like here in a little bit, but essentially we have just replaced about 20 lines of code, boilerplate code nonetheless that we have to always go look up and rewrite, with one line of code, and if that alone doesn't show the power of the Spring Data framework, I'm not sure what will, because that reduction of boilerplate code not only equates to time, but it also reduces the amount of mistakes that you can make when writing this code.
I mentioned a little bit earlier about the ability to swap out datasources, and in my world this is a huge benefit for me, I can do local development using any data source that I happen to have on my machine and when it's time to productionalize that code we can very simply swap out the data source behind the scenes and turn this into production-ready code that a developer can put into our production ecosystem. Spring Data also allows you to focus on the business logic, we just saw that massive reduction of code that occurs, by reducing that code we can focus on what actually matters to our customers, and that's the business logic used to solve those problems that our customers are wanting us to do.
By leveraging Spring Data, we're focusing on their logic and not all of that boilerplate data access code. When you're leveraging the Spring Data framework there's really two key elements that you need to know, the first is the repository and this is an interface that goes back to our repository data pattern. The second is the entity, and that is the object that represents the table in a traditional RDBMS world or a document or other such data structure for all of our NoSQL database technologies.
- Creating a project with Spring Initializr
- Examining the Spring Boot skeleton project with Spring Data JPA
- Creating an embedded database for Spring Boot development and testing
- Building a service abstraction
- Using the IoC paradigm in Spring to develop a service layer
- Building a controller
- Testing a controller MockMvc
- Exposing a service layer through REST