Springboot integration Mybatis plug and use PageHelper

mybatis using RowBounds paging, very convenient, no need to write limit in the sql statement, sql mybatis automatically stitching, adding limit the core is in the mapper interface layer, when the incoming RowBounds mass participation (int offset, int limit) objects to complete pagination

offset: pageNum (p few, can automatically go to the next page) (If this parameter is configured pageHelper still just looking for the next record)

limit: pageSize (each page shows the number of records)

Note: Since the maximum allowable integer java 2147483647, so limit the maximum integer that can be used are 2147483647, disposable remove large amounts of data may cause a memory overflow, so caution limit used in the case of large data

Conclusion: RowBounds biggest advantage is saving the best re-assembled without limit limit in xml statement, but with PageHelper use RowBounds objects

1. The introduction of packet-related github

   		<dependency> //spring导入这个
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        <dependency> //springboot导入这个
	    	<groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper-spring-boot-starter</artifactId>
	    	<version>1.2.3</version>
		</dependency>
        

2. placed application.yml

pagehelper:
    helperDialect: mysql //自动检测当前的数据库链接,自动选择合适的分页方式 也即说明方言为mysql
    reasonable: true //对页码小于0的时候有帮助
    page-size-zero: true //当页码为0的时会查找所有记录
    supportMethodsArguments: true
    params: count=countSql
    offset-as-page-num: true //将offset作为pagenum 即实现了自动翻页
 

3. Important
PageHelper.startPage method Important
only immediately after the first PageHelper.startPage method (Select) method Mybatis inquiry will be paged .

Do not configure the plurality of plug-in tabs
do not configure the plurality of plug-in paging systems (using Spring, mybatis-config.xml and Spring configuration, select one, not simultaneously a plurality of plug-in tabs)!

Pagination plugin does not support paging for update statement with
respect for update sql with the exception, for such recommendations sql manual page throws run-time, after all this sql needs attention.

Pagination plug-in does not support nested result mappings
due to the results of a nested approach will lead to the result set is collapsed, so the query results page after folding the total number will be reduced, it can not guarantee the correct number of paged results.

4. The code implementation

1. Sort query does not use pagehelper

    List<Route> routeList = routeMapper.findByPage(5, "%北京%", 0, 5);

sql statement (use limit) and the method definition mapper

    @Select("select * from tab_route where cid = #{param1}   AND rname LIKE  #{param4}  LIMIT #{param2} ,#{param3} ")
    public List<Route> findByPage(int cid, int start, int pageSize, String rname);

2. Use pagehelper way call with rowbound

     List<Route> routeList = routeMapper.findByPage(5, "%北京%", new RowBounds(0, 5));

sql statement and mapper method definition

    @Select("select * from tab_route where cid = #{param1}  AND rname LIKE  #{param2} ")
    public List<Route> findByPage(int cid, String rname, RowBounds rowBounds);

Results rowBounds (0,5) and when rowBounds (2,5) as said earlier time
if the application.yml arranged offset-as-page-num = true will automatically flip
Here Insert Picture Description

Figure 1. rowBounds (0,5)
row

FIG. 2
above are rowBounds (2,5) can be seen from this is the record of the database 6 and later is the second page (because the content of page 0 and page 1 are the same as shown before recording five)

  1. The method employed PageHelper PageHelper.startPage paging query class can paging ()
    wherein is followed by the first method will be paged select (select not necessarily have to begin at the beginning of widget may find automatically determines whether the operation is a query)
      //分页查询采用PageHelper 能分页 /紧跟着的第一个select方法会被分页
        PageHelper.startPage(2, 5);
        List<Route> routeList = routeMapper.selectByPage(5, "%北京%");//考察是不是只有select开头的方法才可以 其实并不是

sql statement and mapper method definition

  @Select("select * from tab_route where cid = #{param1}  AND rname LIKE  #{param2} ")
    List<Route> selectByPage(int cid, String rname);

As a result FIGS Tulio

  1. Paging query parameters using the method call
        List<Route> routeList = routeMapper.findByPage(5, "%北京%", 0, 5);

sql statement and mapper method definition

    //参数方法的方式调用
    @Select("select * from tab_route where cid = #{param1}  AND rname LIKE  #{param2} ")
    public List<Route> findByPage(
            @Param("cid") int cid,
            @Param("rname") String rname,
            @Param("pageNum") int pageNum,
            @Param("pageSize") int pageSize
    );

As a result FIGS Tulio

Further note that:
not configured
offset-as-page-num:
true as
query rowBounds (0,5) and rowBounds (2,5) when

Or using
PageHelper.offsetPage ();
The
query PageHelper.offsetPage (0, 5) and PageHelper.offsetPage (2, 5)
that when the query does not automatically but merely looking downwardly tab few records 2

Here Insert Picture Description
Figure 3. rowBounds (0,5)

Here Insert Picture Description
Figure 4. rowBounds (2,5) comparison can clearly see FIG. 2 but not only to find the tab down two records

Bug:
will complain Style rowbound methods and parameters methods exist
Here Insert Picture Description
to resolve rowbound methods and parameters OPTION method with a method wherein a call can be selected from two

Reference:
Important

mybatis using difference pagehelper-spring-boot-starter and the pagehelper

Usage - Official Site

Published 81 original articles · won praise 19 · views 3626

Guess you like

Origin blog.csdn.net/c22cxz/article/details/104356686