PageInterceptor分页插件
一、加入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
二、配置文件
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="com.laoli.model"></property>
<!-- 扫描映射文件 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
<!-- 传入PageHelper的插件 -->
<property name="plugins">
<array>
<!-- 传入插件的对象 -->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
三、使用
在service层进行设置
public List<User> findAll(int currPage,int size) {
// TODO Auto-generated method stub
//获取第1页,10条内容,默认查询总数count
//PageHelper.startPage(1, 10);
PageHelper.startPage(currPage, size);
return dao.findAll();
}
在controller层进行使用
@RequestMapping(value = "/findAll")
public String test(Model model,@RequestParam("currPage") int currPage,@RequestParam("size") int size) {
List<User> users=service.findAll(currPage,size);
//PageInfo就是一个分页Bean
PageInfo<User> info=new PageInfo<>(users);
model.addAttribute("users", info);
return "success";
}