Spring Boot Learning (vii) Web App uses SPRING-DATA-JPA to make data access

Source: Internet
Author: User

In the actual development process, the operation of the database is nothing more than "additions and deletions to check." In terms of the most common single-table operations, statements are similar except for tables and fields, and developers need to write a number of similar and tedious statements to complete business logic.

To solve these tedious data manipulation statements, we first thought of using an ORM framework, such as Hibernate. By integrating hibernate, we eventually map data changes to database tables in the way that Java entities are manipulated.

In order to solve the basic "additions and deletions" operation of the abstract Java entities, we usually encapsulate a template DAO for abstraction simplification in generic way, but it is still not very convenient, we need to write an interface that inherits from the generic template DAO for each entity, and then write the implementation of the interface. While some basic data access can be reused, there is a bunch of DAO interfaces and implementations for each entity on the code structure.

For the general application of various types of simple additions and deletions, spring data provides a direct query based on the proxy method, nothing needs to do, the only need to write interfaces, naming methods, this part is too convenient, and simple query to solve almost 80% of the problem. This part is relatively simple, do not repeat, refer to the use of the Daquan section.

Because of the implementation of the template DAO, so that the DAO layer of these concrete entities has become very "thin", there are some concrete entities of the DAO implementation may be completely a simple proxy to the template DAO, and often such an implementation class may appear on many entities. The advent of SPRING-DATA-JPA can make such a "thin" data access layer the only way to write a layer of interfaces. For example, the following example:

01 Import Org.springframework.data.jpa.repository.JpaRepository;
02 Import Org.springframework.data.jpa.repository.Query;
03 Import Org.springframework.data.repository.query.Param;
04
05 /**
06 * Xiao Jin GG

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.