pageHelper----Mybaits 分页插件
=========================================================================
LockMapper 接口:
int insertBatch(List<Lock> lockList);
LockMapper.xml 实现接口的配置文件:
<insert id="insertBatch" >
insert into lock
(lockName,price) values
<foreach collection="list" item="l" separator=",">
(#{l.lockname},#{l.price})
</foreach>
=============================================================================
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.5</version>
</dependency>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="cacheEnabled" value="true"/>
</setti
ngs>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
后面一定要紧跟着在查询语句,不能分开,这样会自动帮我们拼 sql 语句
PageHelper.startPage(num,5);//第一个参数是最开始显示的页面,第二个参数是当前页面显示的记录数
List<Lock> list = lS.selectAll();
List<Lock> list = lS.selectAll();
//将查询结果放在 pageiofo 中,这个 pageinfo 里面有很多方法
PageInfo<Lock> listPageInfo = new PageInfo<Lock>(list);
LockController :
在这里插入代码片 @Controller
public class LockController {
@Autowired
lockService lS;
@RequestMapping("/ListAll")
public ModelAndView lc(@RequestParam(value = "id",defaultValue ="1") Integer num)
{
PageHelper.startPage(num,5);
List<Lock> list = lS.selectAll();
//将查询结果放在 pageiofo 中,这个 pageinfo 里面有很多方法
//这里对应 get 方法去 get 小写,就是 pageinfo 里面的对应属性,也可也直接取出来用
PageInfo<Lock> listPageInfo = new PageInfo<Lock>(list);
System.out.println("当前页码:"+listPageInfo.getPageNum());
System.out.println("总页码:"+listPageInfo.getPages());
System.out.println("总记录数:"+listPageInfo.getTotal());
System.out.println("当前页有几条记录:"+listPageInfo.getSize());
System.out.println("当前页的 pageSize:"+listPageInfo.getPageSize());
System.out.println("前一页:"+listPageInfo.getPrePage());
System.out.println("下一页:"+listPageInfo.getNextPage());
//拿到查询结果
System.out.println("结果:"+listPageInfo.getList());
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
mv.addObject("info",listPageInfo);
return mv;
}
}
success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>信息展示页面</title>
</head>
<body>
<table cellpadding="5" cellspacing="0" bgcolor="#ff69b4" border="1">
<tr>
<th>锁的名字</th><th>锁的价格</th>
</tr>
<%--pageinfo 里面有一个 list 属性,封装了我们的查询结果--%>
<c:forEach items="${info.list}" var="lock">
<tr>
<td>${lock.lockname}</td>
<td>${lock.price}</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">
<a href="ListAll?id=1">首页</a>
<a href="ListAll?id=${info.prePage}">上一页</a>
${info.pageNum}
<a href="ListAll?id=${info.nextPage}">下一页</a>
<a href="ListAll?id=${info.pages}">末页</a>
</td>
</tr>
</table>
</body>
</html>
//第二个参数传入连续要显示的页码
PageInfo<Lock> listPageInfo = new PageInfo<Lock>(list,6);//连续展示 6 页
将连续的页码整合成一个 int 数组
int[] navigatepageNums = listPageInfo.getNavigatepageNums();
页面使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
评论