在Mybatis中使用PageHelper实现简单分页

我这里创建的是maven项目

(1)创建mybatis-config.xml

<?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>
	<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
			<property name="dialect" value="mysql" />
		</plugin>
	</plugins>
</configuration>

(2)在applicationContext-dao.xml中添加以下代码

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 -->
		<property name="mapperLocations" value="classpath:mappings/*.xml"></property>
		<property name="configLocation" value="classpath:spring/mybatis-config.xml" />
	</bean>

(3)创建一个IndexController

        @RequestMapping("/indexVideo")
	public String indexVideo(HttpServletRequest request, Integer pageIndex) {
		// 页数一定要放在dao方法之前
		if(pageIndex==null) {
			pageIndex=1;
		}
		Page page = PageHelper.startPage(pageIndex, 2);
		List<VideoInfo> listVideoInfo = videoInfoService.getVideoInfo();
		request.setAttribute("listVideoInfo", listVideoInfo);
		request.setAttribute("pages", page.getPages());
		return INDEX;
	}

(4)来到index.jsp页面

<body>
	<center>
		<h1>保利视频后台管理系统</h1>
		<a href="locaAddVideo">添加资源</a>
		<table style="BORDER-COLLAPSE: collapse; text-align: center;"
			borderColor=#000000 height=40 cellPadding=1 width="70%"
			align="center" border=1>
			<thead>
				<tr>
					<th>图片</th>
					<th>视频名称</th>
					<th>视频类型</th>
					<th>预览视频</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${listVideoInfo}" var="p">
					<tr style="font-size: 18px">
						<td><img alt="" width="150px;" height="150px;"
							src="/static/imgs/${p.videoUrl}"></td>
						<td>${p.videoName}</td>
						<td>${p.typeName}</td>
						<td><a href="indexVideoDetails?id=${p.id}" style='text-decoration:none;'>预览视频</a></td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
		
		<!-- 使用PageHelper分页开始 -->
		<a href="indexVideo?pageIndex=1">首页</a>
		<c:forEach begin="1" end="${pages}" var="p">
			<a href="indexVideo?pageIndex=${p}">${p}</a>
		</c:forEach>
                <a href="indexVideo?pageIndex=${pages}">尾页</a>
                <!-- 使用PageHelper分页结束 -->
        
	</center>
</body>

ok,大功告成!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值