In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces Spring Boot 2.x how to integrate Spring Data JPA, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Why JPA?
Although JPA is a minority, it is elegant enough for ╮ (╯ _ ╰) ╭. Due to the rise of micro-services and the refinement of service granularity, the scenarios of multi-table federation are gradually reduced, and there are more simple single-table queries, which is the strength of JPA. Therefore, in the future practical projects, I will also use JPA to operate the database, of course, Mybatis will also be integrated in the later tutorials, after all, the mainstream ┑ (operational skills) ┍.
Without much nonsense, let's move on to the subject.
Step 1: add dependencies
First of all, what we need to do is to create a new project, and if there are students who don't know how to read the first HelloWorld, then we add the following dependencies to pom.xml:
What we add here is the two dependencies surrounded by the red box, and then there is a simplified lombok dependency for development. If you need to download a lombok plug-in in the idea environment, open the settings panel, follow the figure below and restart idea.
Step 2: configuration file
Find your application.properties file and configure it:
The meaning of some parameters, I have written very clearly in the comments, I will not say any more here.
Step 3: add entity classes
This is one of our simple user entity classes, the @ Entity annotation is used to generate the database table, @ Table is used to specify the name of the table, @ SequenceGenerator is the rule for the seq_user of the production primary key, @ GeneratedValue is used to generate the primary key according to the rule, and @ Id indicates that this is a primary key.
Step 4: write a simple test program
Repository:
Yes, you read it correctly, this is my interface for operating the database, but why is there nothing? don't panic, let's take a look at what methods the inherited interface provides us with:
As you can see, most of the methods we will use are provided, so what if we don't have what we want in these methods, and we need to write them ourselves? The syntax in the following table basically helps us to do this:
If this doesn't meet our needs, we can also write statements similar to HQL to query:
With regard to the syntax of HQL, we will explain ψ ('∇') ψ separately in a single chapter.
Service:
Interface classes are no longer posted and take up space.
Controller:
After running the program, you can see that the user table and user index table have been created for us.
For the access address, you can see:
Everything is just as we expected. ◕ the ◕.)
Step 5: add paging function
Careful students may have noticed that the interface I inherited can achieve custom sorting and paging functions, so how should we use it?
One line of code solves paging ~ (we may not use it this way in actual combat, but here is just a demonstration of how powerful JPA is)
About Spring Boot 2.x how to integrate Spring Data JPA to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.