使用PageHelper犯的错
以为pagehelper很简单,直接将全部查询结果全部给与pageInfo就行了,结果犯了大错,,分页结果显示不出来,只是将全部结果显示出来。
解决方案:将SQL查询语句的返回值放在pageHelper.startPage(pageNum,pageSize)后面
PageHelper
.startPage(pageNum
, pageSize
);
List
<Video> videos
= videoService
.findAll();
PageInfo pageInfo
= new PageInfo(videos
);
pageHelper一些参数:
public class PageInfo<T> implements Serializable {
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
= false;
private boolean isLastPage
= false;
private boolean hasPreviousPage
= false;
private boolean hasNextPage
= false;
private int navigatePages
;
private int[] navigatepageNums
;
private int navigateFirstPage
;
private int navigateLastPage
;
}
pagehelper的使用
第一步:导入依赖
<dependency>
<groupId>com.github.pagehelper
</groupId>
<artifactId>pagehelper
</artifactId>
<version>5.1.10
</version>
</dependency>
第二步:配置到spring整合配置文件applicationContext.xml中或者mybatis配置文件中
<bean name="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="plugins">
<set>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql
</prop>
<prop key="reasonable">true
</prop>
<prop key="supportMethonsArguments">true
</prop>
</props>
</property>
</bean>
</set>
</property>
</bean>
第三步:controller层直接调用
@RequestMapping("list")
public String
list(@RequestParam(value
= "pageNum",required
= false,defaultValue
= "1") Integer pageNum
,
@RequestParam(value
= "pageSize",required
= false,defaultValue
= "3") Integer pageSize
,
Model model
){
PageHelper
.startPage(pageNum
, pageSize
);
List
<Video> videos
= videoService
.findAll();
for (Video video
: videos
) {
System
.out
.println(video
);
}
PageInfo pageInfo
= new PageInfo(videos
);
model
.addAttribute("pageInfo",pageInfo
);
return "/list.jsp";
}
自动的对PageHelper.startPage 方法下的第一个sql 查询进行分页 PageHelper.startPage(pageNum,pageSize); //紧跟着的第一个select 方法会被分页 List videos = videoService.findAll();
也就是说再controller层PageHelper.startPage(pageNum,pageSize);语句后一定是紧跟查询语句。