我这里创建的是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,大功告成!