In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how SpringBoot integrates Mybatis and uses general mapper and PageHelper for paging, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
1. First introduce the dependencies we need:
General mapper
PageHelper
Mybatis
Configuration of 2.mybatis
There are two ways to configure myabtis, one is through javaconfig configuration, and there is a configuration file that directly uses Springbot: mybatis.mapper-locations=classpath:mapper/**/*.xml, but I use mybatis annotation here, so even this does not need to be configured. In addition, we need to configure the property mapUnderscoreToCamelCase, which is used when we check or add. Mybatis will automatically convert our entity classes to the underscore format in the database according to the hump format. The advantage is not to write resultmap.
3. Integration of General mapper
First, we need to define our own generic mapper interface that inherits the encapsulated two interfaces (both in tk)
MyMapper.java
Next I inherit our corresponding mapper from this mapper, using mapper annotations
FrameCodeMapper.java
Here I only write a deletes method for batch deletion, but this mapper already has all the functions of adding, deleting, modifying and querying, where FrameCode is our corresponding entity in the data table. Now that we have mapper, let's see how to use them.
FrameCodeService.java
We have called mapper in the service layer, and we can see that none of these methods are defined in mapper, indicating that we can directly use the methods in the general mapper, and we do not need to write any repetitive code for adding, deleting, modifying and checking. For the crud method encapsulated in mapper, I focus on only one query (fuzzy query), which is perhaps the most common method used and the most troublesome method used in mapper. I encapsulate it here:
4. Fuzzy query method in original mapper
Mapper of fuzzy query
You can see that here you need to prepare criteria to create a fuzzy query, but here is only a field of fuzzy query. If you add fields later, you need to add them here again and again. Is it very troublesome, and it is not easy to transplant? so I simply encapsulate him here, using reflection. Since I know reflection, many functions have been used, which is really convenient.
MapperUtils.java
So far, I have introduced the integration of mybatis and the use of general mapper, and finally introduced the use of a plug-in PageHelper
Integration and use of 5.PageHelper
When we work on the project, we can't avoid paging. We will do the next paging statement ourselves. Mysql will use limit,sqlserver, top,oracle and rownumber. Will it be very inconvenient? today, I would like to introduce to you the use of PageHelper plug-in in Springboot.
First introduce the dependency of the plug-in
Pom of PageHelper
Then configure pagehelper, and there are also two ways: I'll post them all.
Using the configuration file of springboot
Configure using javaconfig mode
Finally, here's how to use it in a project:
1. First get the pageCurrent (current page) and pageSize (page size) from the request parameters, because I used the bjui framework in the front end, so I received these two fields in the background.
two。 We put the request parameter PageHelper.startPage (pageCurrent,pageSize); in here.
3. Then query the data we need, for example: List entitys = service.getAll (entity)
4. Finally, the data is encapsulated into PageInfo pageInfo=new PageInfo (entitys); here
The data we want is stored in pageInfo, and the total,start,limit,size is all in it. We just need to remove the data we care about. I take out the total number of records of the data here, pageInfo.getTotal (). The total code record is as follows
Thank you for reading this article carefully. I hope the article "how SpringBoot integrates Mybatis and uses General mapper and PageHelper for pagination" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.