MyBatis之分頁插件(pagehelper)

分頁插件步驟

pom.xml

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>

在mybatis配置文件中添加分頁插件

順序: (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,
objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?

<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<property name="dialect"value="mysql"/>   
</plugin>
		<!--5.0版本pagehelper-->
		<!-- <plugin interceptor="com.github.pagehelper.PageInterceptor">
				<property name="helperDialect" value="mysql"/>
		</plugin> -->
</plugins>

測試類中使用PageHelper分頁查詢

     //從第幾頁開始,每頁幾條
       PageHelper.startPage(1, 3);
		List<Pets> list1  = mapper.selectByExample(null);
		//PageInfo封裝結果集
		PageInfo<Pets> pageInfo = new PageInfo<Pets>(list1);
		System.out.println(list1);
		//通過pageInfo可以得到分頁的信息
		List<Pets> list2 = pageInfo.getList();
		System.out.println(list2);
		for (Pets pets : list2) {
			System.out.println(pets);
		}
   System.out.println("每頁幾條:"+info.getPageSize()+" 第幾頁"+info.getPageNum()+" 總的記錄數:"+info.getTotal()+" 總頁數:"+info.getPages());


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章