1.在pom.xml中添加如下:
<properties>
<pagehelper-version>5.0.0</pagehelper-version>
<jsqlparser-version>0.9.5</jsqlparser-version>
</properties>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
<!-- pagehelper的依赖包:jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>${jsqlparser-version}</version>
</dependency>
2.在spring-mybatis中添加如下
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:mapping/**/*.xml"></property>
<!--pageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
helperDialect=postgresql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
3.在controller中
@RequestMapping("pageHelperTest")
public void getList(){
userService.test();
}
在service中
public void test(){
PageHelper.startPage(1, 5);
ArrayList<Users> list = userDao.getUserList();
PageInfo<Users> page = new PageInfo<Users>(list);
System.out.println("总数量:" + page.getTotal());
System.out.println("当前页查询记录:" + page.getList().size());
System.out.println("当前页码:" + page.getPageNum());
System.out.println("每页显示数量:" + page.getPageSize());
System.out.println("总页:" + page.getPages());
}
在dao中
ArrayList<Users> getUserList();
在mapping.xml中
<select id="getUserList" resultType="com.hanpeng.entity.Users">
select * from tb_users
</select>