首先在pom.xml中添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.5</version>
</dependency>
然后在spring-mybatis.xml中添加如下配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/anxyj/mapping/*.xml">
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
offsetAsPageNum=true
rowBoundsWithCount=true
pageSizeZero=true
reasonable=false
supportMethodsArguments=false
returnPageInfo=none
</value>
</property>
</bean>
</array>
</property>
</bean>
运用:
public List<User> showAll(int page, int pageSize) {
PageHelper.startPage(page, pageSize);
List<User> users = userDao.selectAll();
return users;
}
PageHelper.startPage(1, 10);括号中的参数与sql语句中的limit不同,这里表示展示第一页的十条数据
效果