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());


猜你喜欢

转载自blog.csdn.net/weixin_42496678/article/details/82906626