第一步:引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
第二步:mybatis-config.xml配置文件加载
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
第三步:插件配置
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用MySQL方言的分页 -->
<property name="helperDialect" value="mysql"/><!--如果使用mysql,这里value为mysql-->
<property name="pageSizeZero" value="true"/>
</plugin>
</plugins>
第四步:使用
PageHelper.startPage(1, 15);
List<Datainfo> datainfos = service.selectAll();
PageInfo<Datainfo> pageInfo = new PageInfo<>(datainfos);
List<Datainfo> list = pageInfo.getList();
return list;
}
pageInfo.getList();得到分页后的list。