1. What is spring data JPA
Spring Data JPA is a set of JPA application frameworks encapsulated by spring based on the ORM Framework, JPA specification, which enables developers to access and manipulate data with minimalist code. It provides common functions including additions and deletions, and is easy to expand. Learning and using spring data JPA can greatly improve development efficiency.
basically all crud can depend on it for implementation.
2. How to use
adding dependencies in Pom.xml
DAO inheritance Jparepository
Spring Data JPA defaults to a number of basic curd methods, such as: Add, delete, change, etc. The default methods are:
the important thing about spring data JPA is that you can customize a simple query by automatically generating SQL based on the method name, as shown in the following example:
The SQL statement that is generated by the method on the diagram above is:
SELECT * from Tb_banner where shop_id =. 1 and flag =? 2 ORDER BY priority ASC a much longer way ...
The SQL statement that is generated by the method on the diagram above is:
SELECT * from tb_course where shop_id = 1 and one_classify_id =? 2 and two_classify_id =? 3 and flag =? 4 specific keywords, using methods and production into SQL as shown in the following table
Page-Search Spring Data JPA has helped us implement pagination functionality, in the query method, we need to pass in the parameter pageable, in general I will pageable as the last parameter. How to use
The Pageable object can be encapsulated directly in the controller layer.
@GetMapping ("findbanners") public
page<banner> findbanners (@PageableDefault (sort = {"Priority"}, DIRECTION=SORT.DIRECTION.ASC) pageable pageable)
If the front end does not pass the two parameters of page and size, then @pageabledefault defaults to page 1th and the maximum number of bars per page is 10. Note Page 0 is the first page.
Next, call directly at the service level
The Page object returned to the client whose JSON format is
{
"Content": [],//Data contents
"First": whether true,//is page one
"Last": true,//is the final page
"Number": 0,//current Page
"numberOfElements": 0,//The actual number of data bars in the current page
"Size": 0,//Maximum number of pages
"Sort": {},//Ordering information
"Totalelements": 0,//Total number of articles
"TotalPages": 0//Total Pages
}
You can also manually create pageable objects yourself
At the same time, the values contained in the page can be modified by the Page.map method.
The Convert method converts each course in the course collection to Coursesimplevo
Tocoursesimplevo method converts course class to Coursesimplevo class
Dynamic Query
Now that you have the following figure, you need to search for orders through different query criteria.
Because the number of query parameters is indeterminate, we will write many methods on the DAO layer, which is very unsatisfactory.
There are 5 uncertain query parameters in the way of permutation and combination, we may need more than 30 kinds of query methods, as shown in the following figure
So here's a solution that leverages the specification interface in spring data JPA
For the above questions, we create a Orderspec class that creates query parameters
Note: When OrderType is null, it does not appear in SQL query statements
Similarly, we add the other parameters
Time here, do the null value processing
Then in the service layer, you can create a query
Customizing SQL Queries In fact, spring data feels most of the SQL can be implemented according to the method name definition, but for some reason we want to use custom SQL to query, spring data is also the perfect support, in the SQL query method above the use of @query annotation, When it comes to deletion and modification needs to be added @modifying. You can also add @Transactional support to things as needed.
Multi-Table Query
Multi-table queries have two implementations in spring data JPA, the first of which is implemented using hibernate cascading queries, and the second is to create a result set interface to receive the results of a table query, mainly the second way.
the relationship between Spring data JPA and Hibernate, MyBatis, and other persistence-level open source frameworks
My understanding is that although the ORM Framework implements the JPA specification, there are some differences in the code to be written between the different ORM frameworks, and the use of spring Data JPA makes it easier for you to switch between the different ORM frameworks without changing the code. and spring Data JPA is a good encapsulation of the repository layer, which can save a lot of hassle.