第一步我们先导入jar包
地址:
分页jar包地址
第二种方式我们可以实用maven导入:
在 pom.xml 中添加如下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
第二步就是我们在spring中配置,如果你的spring和mabatis是整个好的,配置如下:
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="com.laoli.model"></property>
<!-- 扫描映射文件 -->
<property name="mapperLocations" value="classpath:mybatis/*.xml"></property>
<!-- 传入PageHelper的插件 -->
<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>
一定要在SqlSession的工厂中配置,上面的代码是我的applicationContext.xml中的,也就是整合好之后配置的
因为这是我们国人写的一个mybatis分页插件,用起来非常好用,很简单,它支持基本主流与常用的数据库,例如mysql、
oracle、mariaDB、DB2、SQLite、Hsqldb等。
第三步就是我们如何使用:
public List<User> findAll(int currPage,int size) {
// TODO Auto-generated method stub
//获取第1页,10条内容,默认查询总数count
//PageHelper.startPage(1, 10);
PageHelper.startPage(currPage, size);
return dao.findAll();
}
只需要在我我们查询的时候设置一下PageHelper.startPage()方法
其中的两个参数pageNum,pageSize就是设置页数和条数的
一般这两个参数都是前台传给我们的,不建议写具体的数值
下面的是controller的代码:
@RequestMapping(value = "/findAll")
public String test(Model model,@RequestParam(value = "currPage",defaultValue = "1") int currPage,@RequestParam(value = "size" ,defaultValue = "5") int size) {
List<User> users=service.findAll(currPage,size);
//PageInfo就是一个分页Bean
PageInfo<User> info=new PageInfo<>(users);
model.addAttribute("users", info);
/* for(User user:users) { System.out.println(user); } */
return "success";
}
这个时候我们往前台传达就不是user对象了,而是一个PageInfo,其中PageInfo里面包括user对象
jsp页面如下:
<table class="table table-bordered">
<thead>
<tr>
<th><input type="checkbox" onclick="ChanAll()" id="all"></th>
<th>用户名</th>
<th>用户生日</th>
<th>用户性别</th>
<th>用户地址</th>
<th>用户担任的角色</th>
<th>修改信息</th>
<th>删除用户</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users.list}" var="user">
<tr>
<td><input type="checkbox" name="id" value="${user.id}"></td>
<td>${user.username}</td>
<td>
<fmt:formatDate value="${user.birthday }" pattern="yyyy-MM-dd"/>
</td>
<td>${user.sex}</td>
<td>${user.address}</td>
<td>${user.role.rolename}</td>
<td><a href="to_update?id=${user.id}">编辑</a></td>
<td><a href="#" onclick="dele(${user.id})">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</table>
<nav aria-label="Page navigation">
<ul class="pagination pagination-lg" style="padding-left: 40%">
<li>
<c:if test="${users.pageNum!=1}">
<a href="findAll?currPage=${users.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">上一页</span>
</a>
</c:if>
</li>
<c:forEach begin="1" end="${users.pages}" var="page">
<li><a href="findAll?currPage=${page}">${page}</a></li>
</c:forEach>
<li>
<c:if test="${users.pageNum<users.pages}">
<a href="findAll?currPage=${users.pageNum+1}" aria-label="Next">
<span aria-hidden="true">下一页</span>
</a>
</c:if>
</li>
</ul>
</nav>
pageNum代表当前页,插件为我们自己写好的名字,不能更改。
pages代表总页数,这些都是框架为我们自己写好的。
另外我们我xml文件中的sql查询语句不需要变更,
也不需要写分页语句.
详细的插件使用步骤参考链接如下:
MyBatis 分页插件 - PageHelper