1、maven依賴,注意使用pagehelper時的版本必須與mybatis版本對應(yīng)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!-- 添加mybatis依賴 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version> 3.3 . 0 </version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version> 1.2 . 3 </version> </dependency> <!-- pagehelper --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version> 4.1 . 4 </version> </dependency> |
2、需要在mybatis的配置信息中使用pagehelper插件,mybatis-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <plugins> <plugin interceptor= "com.github.pagehelper.pagehelper" > <!--指明數(shù)據(jù)庫 4.0 . 0 以后不需要設(shè)置此屬性--> <property name= "dialect" value= "mysql" /> <!-- 該參數(shù)默認為 false --> <!-- 設(shè)置為 true 時,會將rowbounds第一個參數(shù)offset當成pagenum頁碼使用 --> <!-- 和startpage中的pagenum效果一樣--> <property name= "offsetaspagenum" value= "true" /> <!-- 該參數(shù)默認為 false --> <!-- 設(shè)置為 true 時,使用rowbounds分頁會進行count查詢 --> <property name= "rowboundswithcount" value= "true" /> <!-- 設(shè)置為 true 時,如果pagesize= 0 或者rowbounds.limit = 0 就會查詢出全部的結(jié)果 --> <!-- (相當于沒有執(zhí)行分頁查詢,但是返回結(jié)果仍然是page類型)--> <property name= "pagesizezero" value= "true" /> <!-- 3.3 . 0 版本可用 - 分頁參數(shù)合理化,默認 false 禁用 --> <!-- 啟用合理化時,如果pagenum< 1 會查詢第一頁,如果pagenum>pages會查詢最后一頁 --> <!-- 禁用合理化時,如果pagenum< 1 或pagenum>pages會返回空數(shù)據(jù) --> <property name= "reasonable" value= "true" /> <!-- 3.5 . 0 版本可用 - 為了支持startpage(object params)方法 --> <!-- 增加了一個`params`參數(shù)來配置參數(shù)映射,用于從map或servletrequest中取值 --> <!-- 可以配置pagenum,pagesize,count,pagesizezero,reasonable,orderby,不配置映射的用默認值 --> <!-- 不理解該含義的前提下,不要隨便復(fù)制該配置 --> <property name= "params" value= "pagenum=start;pagesize=limit;" /> <!-- 支持通過mapper接口參數(shù)來傳遞分頁參數(shù) --> <property name= "supportmethodsarguments" value= "true" /> <!-- always總是返回pageinfo類型,check檢查返回類型是否為pageinfo,none返回page --> <property name= "returnpageinfo" value= "check" /> </plugin> </plugins> </configuration> |
3、在配置spring配置文件中,配置mybatis的sqlsessionfactory時,需要把mybatis-config.xml添加到屬性中
1
2
3
4
5
6
7
8
9
|
<!-- sqlsessionfactory --> <bean id= "sessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" > <!--設(shè)置數(shù)據(jù)源--> <property name= "datasource" ref= "datasource" ></property> <!--設(shè)置映射文件--> <property name= "mapperlocations" value= "classpath:mybatis/sqlmap/mapper/*.xml" ></property> <!--設(shè)置pagehelper--> <property name= "configlocation" value= "classpath:mybatis/mybatis-config.xml" ></property> </bean> |
4、使用mybatis的mapper接口進行查詢,在查詢時,需要使用pagehelper.startpage方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@test public void testselectall() { page<doctor> page = pagehelper.startpage( 1 , 3 ); //selectall查詢出的list即為上面定義的page doctormapper.selectall(); //注意: //使用pagehelper.startpage只是針對接下來的一條查詢語句, //如果又查詢了一次數(shù)據(jù),則還需要使用一次pagehelper.startpage logger.info( "獲取所有doctor信息,獲得記錄數(shù):{}" , page.size()); logger.info( "獲取所有doctor信息,獲得記錄:{}" , page); //使用pageinfo封裝 pageinfo<doctor> info = new pageinfo<doctor>(page); logger.info( "info.getpages:{}" ,info.getpages()); } |
5、page對象繼承了arraylist,因此在使用了pagehelper.startpage之后,page即為查詢到的數(shù)據(jù),并且在page中還額外封裝了pagenum,pagesize等屬性,還可以使用pageinfo封裝page,pageinfo中有更多的分頁屬性,例如isfirstpage是否為首頁、islastpage是否為末尾、hasnextpage是否存在下一頁等。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/cksvsaaa/p/6036068.html