期末大作业的前端也要自己写,不会用分页,于是百度了一下,发现百度知道里也是有好东西的,在里面看到了pagehelper分页插件的用法,记录下来,以后可能用得到。
先在pom.xml中导入包
<!-- pageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
在Spring-mybatis.xml文件中加入相关配置
<!-- MyBatis文件配置,扫描所有.xml文件 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatis-config.xml"
p:mapperLocations="classpath:com/nenu/newsManage/*.mapper.xml">
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
<property name="properties">
<value>
<!--使用的数据库类型-->
dialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
配置完成,开始使用
在ccontroller中配置
@RequestMapping(value = {"/", "index"}, method = RequestMethod.GET)
public String toIndex(@RequestParam(required = false, defaultValue = "1") int pageNo,
@RequestParam(required = false, defaultValue = "10") int pageSize,
@RequestParam(required = false, defaultValue = "1") int columnId,
Model model) throws Exception {
//开始分页,分页参数是页码和每页记录数
PageHelper.startPage(pageNo, pageSize);
List<Essay> essayList = essayService.listEssayByColumnId(pageNo,
pageSize,
columnId);
//对象列表传入页面
PageInfo<Essay> page = new PageInfo<Essay>(essayList);
List<Columns> columnList = columnService.listColumn();
Columns column = columnService.queryColumnById(columnId);
model.addAttribute("essayPage", page);
model.addAttribute("columnList", columnList);
model.addAttribute("column", column);
return "index";
}
service:没用listByPage方法
@Override
public List<Essay> listEssayByColumnId(int pageNo,
int pageSize,
Integer columnId) throws Exception {
List<Essay> essayList = essayMapper.listEssayByColumnId(pageNo, pageSize, columnId);
return essayList;
}
mapper里也是正常查询,没有limit
<select id="listEssayByColumnId" resultMap="essayResultMap">
SELECT *
FROM t_essay
LEFT JOIN t_column
ON t_essay.column_id = t_column.column_id
WHERE t_essay.column_id = #{columnId}
AND t_essay.essay_state = 1
ORDER BY t_essay.essay_id ASC
</select>
index.jsp
<!-- 页面列表部分 -->
<ul class="nei_1">
<c:forEach items="${essayPage.list}" var="essay">
<li id="line_u7_0">
<a href="essayDetail.jsp?eid=${essay.essayId}"
target="_top" title="${essay.essayName}" indepth="true">${essay.essayName}</a>
</li>
<span id="section_u7_0" style="display: none;">
<hr style="height: 1px; border-width: 1px medium medium; border-style: dashed none none; border-color: rgb(204, 204, 204) currentcolor currentcolor; -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none;">
</span>
</c:forEach>
</ul>
<!-- 分页页码部分 -->
<div id="pageNum">
<c:if test="${essayPage != null && essayPage.getTotal() > 0 }">
<nav style="text-align: center">
<ul class="pagination pagination-lg">
<li><a>共 ${essayPage.total } 条记录</a></li>
<li><a>当前第 ${essayPage.pageNum} 页</a></li>
<c:if test="${essayPage.pageNum!= 1 }">
<li><a href="?columnId=${column.columnId}&pageNo=${essayPage.pageNum - 1}">上一页</a></li>
</c:if>
<c:if test="${essayPage.pageNum < essayPage.pages }">
<li><a href="?columnId=${column.columnId}&pageNo=${essayPage.pageNum + 1}">下一页</a></li>
</c:if>
<li><a>共 ${essayPage.pages} 页</a></li>
</ul>
</nav>
</c:if>
</div>
之后就能正常使用了