Spring Data JPA development manual-1. Introduction

Source: Internet
Author: User

Spring Data JPA development manual-1. Introduction
Spring Data JPA development manual-1. introduction I found that there were almost no articles on the Internet about the principles of Spring Data JPA. Many guides are about how to configure them first, followed by several pieces of code, there is no article that makes the principles and composition of Spring Data JPA clear. So I wrote this series of articles.
Using the Java Persistence API library to create a persistent layer is cumbersome and time-consuming, and requires a large amount of sample code. To remove some sample code, follow these steps:
1) Create an abstract base class that provides CRUD operations on the object.
2) create a specific operation class that inherits this base class.
The problem with this method is that we still need to write code for creating databases to query and call them. What's worse, when we want to create a new database query, we still need to do this once. This is a waste of time.
If I tell you, we can create a JPA persistence layer without writing any sample code. Do you believe this?
Spring Data JPA can help us achieve this. The official Spring Data JPA website claims:
The data access layer of the application has always been very cumbersome. It always needs to write a lot of sample code to execute simple queries, such as paging and auditing. Spring Data JPA aims to improve the implementation of the Data access layer and reduce development time. When developers write database interfaces, including custom query methods, Spring Data JPA will automatically provide its implementation.
This article introduces Spring Data JPA. Let's get started.
1. What is Spring Data JPA?
Spring Data JPA is not a JPA implementation. It is a framework or library that provides an additional abstraction layer on the JPA Provider. If we decide to use Spring Data JPA, the backend part of the application will contain at least three layers:
1) Spring Data JPA
Spring Data JPA provides the JPA Provider abstraction layer.
2) Spring Data Commons
Spring Data Commons provides basic components for sharing.
3) JPA Provider
Java Persistence API implementation.
It seems that Spring Data JPA makes our applications more complex. In some ways, it is true. However, we need to understand that Spring Data JPA reduces the time required to write sample code.
Structure of the Project persistence layer:


II. Introduction to Spring Data Repositories
Spring Data JPA depends on Spring Data Commons, which is a database abstraction layer that provides shared basic database components.
We do not need to pay attention to the implementation of any database abstraction layer when using Spring Data JPA, but we must be familiar with the Spring Data database interface. These interfaces are described as follows:
1. Spring Data Commons provides the following interfaces:
1) Repository <T, ID extends Serializable> Interface
It is a tag interface with two goals:
First, capture the types of hosted entities and object IDS
The second is to help the Spring container find the specific data warehouse interface during the class path scan.
2) CrudRepository <T, ID extends Serializable> Interface
Provides CRUD operations on managed entities.
3) PagingAndSortingRepository <T, ID extends Serializable> Interface
Provides paging and sorting methods for entities retrieved from the database.
4) QueryDslPredicateExecutor <T> Interface
This interface is not a data warehouse interface. It declares the method for retrieving the database by querying the DSL predicate object.
2. Spring Data JPA provides the following interfaces:
1) JpaRepository <T, ID extends Serializable> Interface
It is a specific data warehouse interface of JPA and a combination of methods for declaring a single interface behind the Commons Data Warehouse interface.
2) JpaSpecificationExecutor <T> Interface
This interface is not a data warehouse interface. It declares the method for retrieving entities from the database by using the Specification <T> Object of the JPA standard API.
The data warehouse hierarchy looks like:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.