Spring Data JPA Development manual--1. Introduction

Source: Internet
Author: User

"Spring Data JPA Development Manual--1. Introduction"

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

I find that there are few articles on the web that tell the principles of spring data JPA, many of which are configured in the first place, followed by a few large pieces of code, and no article can explain the principles and composition of spring data jpa. So, I wrote this series of articles.

Creating a persistence layer with the Java Persistence API library is tedious, time consuming, and requires a lot of boilerplate code. We can eliminate some boilerplate code by following these steps:

1) creates an abstract base class that provides CRUD operations on entities.

2) Create a specific action class that inherits this base class.

The problem with this approach is that we still have to write the code that creates the database queries and calls them. What's worse, we're going to do this again when we're going to create a new database query. It's a waste of time.

If I told you that we could create a JPA persistence layer without having to write any boilerplate code, would you believe it?

Spring Data JPA can help us do this. The Spring Data JPA website claims:

The implementation of the data access layer of the application is always cumbersome, and there are many boilerplate code to execute simple queries, such as paging and auditing. Spring data JPA is designed to improve the implementation of the data access layer and reduce development time. When a developer writes a database interface, including a custom query method, Spring Data JPA automatically provides its implementation.

This article introduces Spring Data JPA, let's get started.

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

One, what is Spring Data JPA

Spring Data JPA is not a JPA implementation, it is a framework or library that provides an additional layer of abstraction on top of the JPA provider. If we decide to use Spring Data JPA, then the back-end portion of the application will contain at least three layers:

1) Spring Data JPA

Spring Data JPA provides the abstraction layer for JPA provider.

2) Spring Data Commons

Spring Data Commons provides shared infrastructure artifacts.

3) JPA Provider

Implementation of the Java persistence API.

It seems that spring Data JPA makes our applications more complex, and in some ways it does. But understand that Spring Data JPA reduces the time we write boilerplate code.

Structure of the Project persistence layer:


Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

Two, Spring Data repositories Introduction

Spring data JPA relies on spring data commons--, which is a database abstraction layer that provides shared database infrastructure artifacts.

When we use spring data JPA, we do not need to focus on the implementation of any database abstraction layer, but we must be familiar with the spring data database interface. These interfaces are described as follows:

1, Spring Data Commons provides the following interface

1) repository<t, ID extends Serializable> interface

It is a labeled interface with two targets:

One is to capture the type of managed entity and the type of entity ID

The second is to help the spring container find the specific data warehouse interface during the CLASSPATH 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 operations for entities retrieved from the database.

4) Querydslpredicateexecutor<t> Interface

This interface is not a data warehouse interface. It declares a way to retrieve a database by querying a DSL predicate object.

2, Spring Data JPA provides the following interface

1) jparepository<t, ID extends Serializable> interface

It is a JPA-specific data warehouse interface that is a combination of methods for the single interface declaration behind the Commons data Warehouse interface.

2) Jpaspecificationexecutor<t> Interface

This interface is not a data warehouse interface. It declares the method of retrieving entities from the database by using the JPA standard API's Specification<t> object.

The hierarchy of the Data warehouse looks like:


Spring Data JPA Development manual--1. Introduction

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.