Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the PageHelper in Spring Boot 2.x that you don't know?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the PageHelper is in Spring Boot 2.x that you don't know. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

PageHelper

Speaking of PageHelper, friends who have used Mybatis may not be very strange, as a paging plug-in developed by Chinese people, it basically meets our daily needs.

I spent a whole night studying how to play this reasonably.

Getting started

If you want to do a quick paging operation in a Spring Boot project, you only need two steps:

Import Maven

Here I am importing the latest official:

Org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 tk.mybatis mapper-spring-boot-starter 2.1.5 com.github.pagehelper pagehelper-spring-boot-starter 1.2.10 uses / / only the query (Select) method of the first Mybatis immediately after the PageHelper.startPage method will be paged! PageHelper.startPage (1,10); return PageInfo.of (userService.findAll ())

Yes, it only takes two lines of code to achieve the paging effect we want.

Advanced play

If you just want to simply use the paging feature, then this article is over for you, but as a programmer, will you be content with rudimentary play? No, no! You will not! So, keep looking down ~

From the documentation, we can see that the author provides us with a lot of parameters for us to configure:

HelperDialect,offsetAsPageNum,rowBoundsWithCount,pageSizeZero,reasonable,params,supportMethodsArguments,autoRuntimeDialect,closeConn and so on, we can get a more powerful effect by configuring these properties.

It is important to note that most of the online tutorials are for us to configure in xml or code. In fact, if you are using springboot, why do you want to go further? we can configure it directly in the configuration file application.yml of Spring boot:

The pagehelper: # dialect: ① # paging plug-in automatically detects the current database link and automatically selects the appropriate paging method (may not be set) helper-dialect: mysql # after the database is set above, the following setting is true will not change the above result (default is true) auto-dialect: true page-size-zero: false # ② reasonable: true # ③ # default is false This parameter is valid when using RowBounds as the paging parameter. (generally not needed) offset-as-page-num: false # defaults to false,RowBounds whether to perform count queries (generally not) row-bounds-with-count: false # params: ④ # support-methods-arguments: used in conjunction with params, as shown in the following explanation # default is false. When set to true, it is allowed to automatically identify the paging auto-runtime-dialect of the corresponding dialect based on multiple data sources at run time: false # ⑤ # in conjunction with auto-runtime-dialect close-conn: true # is used to control whether the count query is executed in the method without count query by default. When set to true here, total will be-1 default-count: false # dialect-alias: ⑥

①: PageHelper is used for paging by default. If you want to implement your own paging logic, you can implement the Dialect (com.github.pagehelper.Dialect) interface, and then configure this property to be the fully qualified name of the implementation class. (this is not recommended here, after all, you use other people's plug-ins, why bother?)

②: the default value is false, and when this parameter is set to true, all results will be queried if pageSize=0 or RowBounds.limit = 0 (equivalent to no paging query is executed, but the returned result is still of type Page).

It needs to be pointed out here that although all the results are returned, the action of count is still performed. The developer has indicated in issue that later versions will fix this problem.

After my test, it has not been solved yet.

③: validity, that is, error correction mechanism. Configure reasonable to true, and if pageNum pages will query the last page.

④: in order to support the startPage (Object params) method, this parameter is added to configure the parameter mapping, which is used to take values from the object according to the property name. PageNum,pageSize,count,pageSizeZero,reasonable can be configured, and the default value of the mapping is not configured.

PageNum=pageNum

PageSize=pageSize

Count=countSql

Reasonable=reasonable

PageSizeZero=pageSizeZero .

Support-methods-arguments supports passing paging parameters through Mapper API parameters. The default value is false. The paging plug-in automatically selects values from the parameter values of the query method according to the fields configured by params above, and paging automatically when the appropriate value is found.

How about a little?

@ GetMapping ("/ page1") public PageInfo findPage (HttpServletRequest request) {/ / directly pass the parameters contained in the request to PageHelper.startPage (request); return PageInfo.of (userService.findAll ()) } / * query all information * @ return personnel list * / @ Select ("SELECT * FROM user") @ Results ({@ Result (property = "userName", column = "user_name"), @ Result (property = "password", column = "password")}) List findAll () The difference between this method and the above method is that the conditional request is passed directly to the Mapper interface @ GetMapping ("/ page2") public PageInfo findPage2 (HttpServletRequest request). {return PageInfo.of (userService.findPage (request)) } / / the difference between Mapper interface and one is that SQL is exactly the same as @ Select ("SELECT * FROM user") @ Results ({@ Result (property = "userName", column = "user_name"), @ Result (property = "password", column = "password")}) List findPage (HttpServletRequest request)

⑤: the default is false. When set to true, allows paging of corresponding dialects to be automatically identified at run time based on multiple data sources

CloseConn: the default is true. When you use the runtime dynamic data source or do not set the helperDialect property to automatically obtain the database type, you will automatically obtain a database connection. Use this property to set whether to close the acquired connection. The default true is closed, and when it is set to false, the acquired connection will not be closed. The setting of this parameter depends on the data source you choose.

⑥: dialect-alias parameter, which allows you to configure aliases for custom implementations, which can be used to automatically obtain corresponding implementations according to JDBCURL, allowing existing implementations to be overwritten in this way. For example, configuration shows (multiple configurations are separated by semicolons):

Several different ways to play pagehelper.dialect-alias=oracle=com.github.pagehelper.dialect.helper.OracleDialect

(whispering BB) Lambda is really pretty!

/ / 1. OffsetPage PageHelper.offsetPage (1, 10); return PageInfo.of (userService.findAll ()); / / 2. Lambda return PageHelper.startPage (1, 10). DoSelectPageInfo (()-> userService.findAll ()); the above is what you don't know about PageHelper in Spring Boot 2.x. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report