Development of web-applications with the help of Spring MVC implies creation of several logical layers of architecture. One of the layers is a DAO (Repository) layer. It is responsible for communication with a database. If you developed the DAO layer at least once, you should know that it involves a lot of boilerplate code. A Spring Data take a part of the routine job related to the DAO on itself.
In the post I’m going to provide an example of application which will demonstrateSpring Data (JPA)in conjunction withSpring MVC, MySQL and Maven.Hibernatewill be used as implementation of the JPA. As you probably know, I’m a real fan of java based configurations, so I will use this approach to configure the Spring Data. In the end of the tutorial you can find a link to the sample project on GitHub.
In the article I want to concentrate on the Spring Data, so all stuff which is out topic I will omit. But in the start I want provide a bulk of links which can be helpful for you in context of this tutorial.
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
Pay your attention at@EnableJpaRepositoriesannotation. It enables usage of JPA repositories. Thecom.spr.repositorypackage will be scaned to detect repositories. In theentityManagerFactorybean I determined that Hibernate will be used as JPA implementation.
Definitely it is the most simplest code snippet in the tutorial. But it requires the most high attention. The JpaRepository interface contains the basic operations which can be performed with any entity (CRUD operations). More information you can find on theofficial documentation page.
The Spring Data is very powerful weapon, it helps you develop an application more faster and avoid hundreds of boilerplate strings of code. Usage of Spring Data is the most convenient way to create a DAO layer in an application, so don’t ignore it in your projects.