目录
2、在Mybatis_config.xml文件中导入分页插件
1、导入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2、在Mybatis_config.xml文件中导入分页插件
要注意插件在mybatis配置文件中的位置,否则会爆红
<!--分页插件 在配置文件中的位置必须要正确,否则会爆红-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
3、编写mapper接口和mapper.xml
接口:
//查询用户list
List<User> finAllUser();
mapper.xml:
<select id="finAllUser" parameterType="int" resultType="User">
select * from user
</select>>
4、编写service层
Service接口:
PageInfo<User> findUserByPage(int Pagenum,int size);
接口实现类:
public PageInfo<User> findUserByPage(int pagenum, int size){
if(pagenum==0)
pagenum=1;//当到最后一页时,其下一页的Pagenum的值为0 所有要给Pagenum赋值
//使用分页插件工具完成分页
PageHelper.startPage(pagenum,size);
//查询所有的用户数据
List<User> list = userDao.finAllUser();
//将所有用户数据传入PageInfo<User>中,其他的属性会根据当前页的数据计算出来
PageInfo<User> pageInfo=new PageInfo<>(list);
return pageInfo;
}
返回值为PageInfo对象 :
1、传入当前页码和每页显示数据的条数
2、 Pageinfo对象中的参数只需要注入所有的用户数据,其他的属性会根据所有的用户数据计算出来。
pageinfo中的所有参数如下:
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据"//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集(每页显示的数据)
private List<T> list;
//第一页
private int firstPage;
//前一页
private int prePage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
5、编写测试类
@Test
public void Pagein(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
PageInfo<User> userByPage = userServiceImpl.findUserByPage(1, 3);
System.out.println("下一页为:"+userByPage.getNextPage());
System.out.println("当前页数据为:"+userByPage.getList().toString());
System.out.println("总的用户数量为:"+userByPage.getTotal());
System.out.println("总的页数为:"+userByPage.getPageSize());
}
6、编写Controller
此Controller是刚进入表单界面,显示分页第一页的数据。
@RequestMapping("/split")//显示第一页数据
public String split(Model model)
{
PageInfo info= userService.findUserByPage(1,3);//第一页 每页三条数据
model.addAttribute("info",info);
return "list";
}
此时访问这个控制器,前端list.jsp界面,从info中解析数据并展示.
7、编写前端界面,实现翻页
同步刷新实现上一页、下一页,和指定页面
${pageContext.request.contextPath}/user/ajaxsplit?page=要翻到的页码
<a href="${pageContext.request.contextPath}/user/ajaxsplit?page=${info.prePage}"
aria-label="Previous">
<span aria-hidden="true">«</span></a>
</li>
<c:forEach begin="1" end="${info.pages}" var="i">
<c:if test="${info.pageNum==i}">
<li>
<a href="${pageContext.request.contextPath}/user/ajaxsplit?page=${i}"
style="background-color: grey">${i}</a>
</li>
</c:if>
<c:if test="${info.pageNum!=i}">
<li>
<a href="${pageContext.request.contextPath}/user/ajaxsplit?page=${i}">${i}</a>
</li>
</c:if>
</c:forEach>
<li>
<a href="${pageContext.request.contextPath}/user/ajaxsplit?page=${info.nextPage}"
aria- label="Next">
<span aria-hidden="true">»</span></a>
</li>
</li>
后台ajaxsplit
@RequestMapping("/ajaxsplit")//分页展示
public String ajaxsplit(int page,Model model)
{
PageInfo info= userService.findUserByPage(page,3);//每页3条数据
model.addAttribute("info",info);
return "list";
}
现在增加一个用户时会增加到最后一页,只需要修改一下Mapper.xml中sql语句
select * from user order by id desc
根据id降序排列