第一步:在pom中导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
第二步: 在 applicationContext.xml 中配置拦截器插件
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.zgw.domain"></property>
<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>
第三步:具体使用
1.在serviceimpl中的代码:
public List<Orders> findall(int page,int size) {
PageHelper.startPage(page,size);
List<Orders> list = ordersDao.findall();
return list;
}
2.在controller中的代码:
@RequestMapping("/findall")
public String findAll(Model model, @RequestParam(name = "page",required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "4") int size){
List<Orders> list = odersService.findall(page,size);
PageInfo pageInfo = new PageInfo(list);
model.addAttribute("pageInfo",pageInfo);
return "orders-page-list";
}
pageHelper的基本使用:
private static final long serialVersionUID = 1L;
private int pageNum;
private int pageSize;
private int size;
private int startRow;
private int endRow;
private long total;
private int pages;
private List<T> list;
private int prePage;
private int nextPage;
private boolean isFirstPage;
private boolean isLastPage;
private boolean hasPreviousPage;
private boolean hasNextPage;
private int navigatePages;
private int[] navigatepageNums;
private int navigateFirstPage;
private int navigateLastPage;