框架:SSM:Spring+SpringMVC+Mybatis
数据库:MySQL
1.前端代码
引入JSTL标签库
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div id="p">
<a href="manager.html?p=1">首页</a>
<c:choose>
<c:when test="${page.currentPage > 1 }">
<a href="manager.html?p=${page.currentPage-1 }">上一页</a>
</c:when>
<c:otherwise>
<a href="#">上一页</a>
</c:otherwise>
</c:choose>
<c:forEach var="p" begin="1" end="${page.pageCount }">
<a href="manager.html?p=${p }">${p }</a>
</c:forEach>
<c:choose>
<c:when test="${page.currentPage < page.pageCount }">
<a href="manager.html?p=${page.currentPage+1 }">下一页</a>
</c:when>
<c:otherwise>
<a href="#">下一页</a>
</c:otherwise>
</c:choose>
<a href="manager.html?p=${page.pageCount }">末页</a>
</div>
2.服务端代码
①控制层Controller
@RequestMapping("manager.html")
public String manager(@RequestParam(value="p", required=false)String currentPage, ModelMap map){
System.out.println("currentPage"+currentPage);
List<User> list = listService.findAllUser();
Page page = new Page();
page.setNumber(5);
if(currentPage!=null){
page.setCurrentPage(Integer.parseInt(currentPage));
}
if(list.size()%5 == 0){
page.setPageCount(list.size()/page.getNumber());
}else{
page.setPageCount(list.size()/page.getNumber() + 1);
}
int pageStart = page.getNumber()*(page.getCurrentPage()-1);//每页开始
System.out.println("page :"+page.toString());
System.out.println("pageStart :"+pageStart);
List<User> pageList = listService.findUsersByPage(pageStart, page.getNumber());
for(User key : pageList){
System.out.println(key.toString());
}
map.addAttribute("users", pageList);
map.addAttribute("page", page);
return "manager";
}
@Service
public class UserListService {
@Resource(name="userListDao")
private UserListDao dao;
public List<User> findAllUser(){
return dao.findAllUser();
}
public List<User> findUsersByPage(int pageStart, int pageEnd){
return dao.findUsersByPage(pageStart, pageEnd);
}
}
@MyBatisRepository
public interface UserListDao {
List<User> findAllUser();
List<User> findUsersByPage(int pageStart, int pageEnd);
}
④映射文件
<mapper namespace="com.chen.dao.UserListDao">
<select id="findAllUser" resultType="com.chen.entity.User">
select
user_id,
username,
password,
email,
telephone,
enrolldate
from
user_info
</select>
<select id="findUsersByPage" parameterType="int" resultMap="pageList">
select
user_id,
username,
password,
email,
telephone,
enrolldate
from
user_info
<if test="_parameter != null">
limit #{0}, #{1}
</if>
</select>
<resultMap type="com.chen.entity.User" id="pageList">
<result property="user_id" column="user_id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="telephone" column="telephone"/>
<result property="enrolldate" column="enrolldate"/>
</resultMap>
</mapper>
⑤Mybatis配置文件
<!-- 扫描指定包下带有注解@MybatisRepository的接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.chen.dao"/>
<property name="annotationClass"
value="com.chen.annotation.MyBatisRepository"/>
</bean>
//用于标识接口,方便MapperScannerConfigurer精确扫描
public @interface MyBatisRepository {
}