1.在pom.xml中
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.2</version>
</dependency>
如果中央仓库没有该JAR包,它会自动下载
2.page.java
package entity;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
public class Page<E> {
private List<E> lsit;
public List<E> getLsit() {
return lsit;
}
public void setLsit(List<E> lsit) {
this.lsit = lsit;
}
private int nextpage;// 下一页
private long total;// 总记录数
private int page;// 请求页
private int prepage;// 上一页
private int lastpage;// 最有益也
private int pagenum;// 当前页数
private int pages;// 总页数
public int getNextpage() {
return nextpage;
}
public void setNextpage(int nextpage) {
this.nextpage = nextpage;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPrepage() {
return prepage;
}
public void setPrepage(int prepage) {
this.prepage = prepage;
}
public int getLastpage() {
return lastpage;
}
public void setLastpage(int lastpage) {
this.lastpage = lastpage;
}
public int getPagenum() {
return pagenum;
}
public void setPagenum(int pagenum) {
this.pagenum = pagenum;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public Page(int nextpage, long total, int page, int prepage, int lastpage, int pagenum, int pages) {
super();
this.nextpage = nextpage;
this.total = total;
this.page = page;
this.prepage = prepage;
this.lastpage = lastpage;
this.pagenum = pagenum;
this.pages = pages;
}
public Page() {
super();
}
public Page<E> getpage(List<E> list)
{
Page<E> pageutil=new Page<E>();
PageInfo<E> info=new PageInfo<E>(list);
pageutil.setLastpage(info.getLastPage());
pageutil.setNextpage(info.getNextPage());
pageutil.setPagenum(info.getPageNum());
pageutil.setPages(info.getPages());
pageutil.setPrepage(info.getPrePage());
pageutil.setTotal(info.getTotal());
return pageutil;
}
}
该类用于存放分页所需要的数据
3.在controller中
*/
@RequestMapping(value = "/getAllFacultyInfo")
public String getAllFacultyInfo(HttpServletRequest request,@RequestParam int page) {
PageHelper.startPage(page, 10);
List<Faculty> facultylist = facultyService.QueryAllFaculty();
//分页
Page<Faculty> pageutil=new Page<Faculty>();
if(facultylist.size()!=0)
{
request.setAttribute("facultyList", facultylist);
request.setAttribute("pageutil", pageutil.getpage(facultylist));
int type=0;
request.setAttribute("type", type);
}
else
{
request.setAttribute("facultyList", null);
}
return "user/faculty";
}
4.配置文件mybatisConfig.xml
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="true"/>
<property name="rowBoundsWithCount" value="true"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
</configuration>
注意plugins的位置
在pring_mybatis.xml中配置
<!-- 会话工厂 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis_config.xml"></property>
</bean>