SSM框架实现 分页功能

这属于好记性不如烂笔头系列,将自己平时的一些小事情记录下来,今天来记录一下如何实现分页功能。

首先用的是SSM框架,SSM框架用的还是比较多的。所以选择还是使用SSM框架,

要实现分页功能其实很简单,sql也很简单,就是 select * from table limit  start end;(数据库使用的mysql)

首先要有页面 可以来一个table标签

首先需要查询数据库,库中需要显示一共有多少个。select count(*) from table;

controller层

@Controller
@RequestMapping("/user")
public class UserAction {
 // private static Logger log = LoggerFactory.getLogger(UserAction.class);

 @Resource
 private UserService userservice;
 
 @RequestMapping(value="/test",method=RequestMethod.GET)
 public String test (HttpServletRequest request,Model model) {
  
  int sumcount = userservice.countnum();   //得到总记录数
  int page = 0;
  int indexnum = 1;                     //页数
  int size = 3;         //每页显示的页数
  //System.out.println(request.getParameter("page"));
  if(request.getParameter("page") != null) {
   indexnum = Integer.parseInt(request.getParameter("page")); //页数
   page = (indexnum-1)*size;
  }
  //System.out.println(page);
  
  HashMap<String,Object> map = new HashMap<String,Object>();
  map.put("page", page);
  map.put("size", size);
  sumcount = sumcount/size;
  List<User> users = userservice.querylist(map);      //查询出所有用户列表,使用list接收
  model.addAttribute("listusers", users);    //modle将数据传输给JSP
  model.addAttribute("sum", sumcount);
  model.addAttribute("indexnum", indexnum);
  return "login";
 }

Service层

我把业务逻辑写在了controller层,所以service层就是相对应的接口和方法,在serviceImpl中 调用dao层的方法。然后对应Mapper里的sql

mapper.xml

<mapper namespace="org.chery.ssm.dao.UserDao">  
<resultMap type="org.chery.ssm.modle.User" id="BaseResultMap">
 <id column="id" property="id" jdbcType="INTEGER"/>
 <result column="user_name" property="username" jdbcType="VARCHAR"/>
 <result column="password" property="password" jdbcType="VARCHAR"/>
 <result column="age" property="age"  jdbcType="INTEGER"/>
</resultMap>

<select id="querylist" resultMap="BaseResultMap"  parameterType="int">
  select * from user_t limit #{page},#{size}
  
</select>
<select id="countnum" resultType="int">
 select count(*) from user_t
</select>

JSP页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>

<h1>欢迎${user.username}</h1>
<hr>
<div>
  <h1>用户列表</h1>
  <table border="1" cellpadding="10" cellspacing="0">
  <thead>
   <tr>
    <td>ID</td>
    <td>姓名</td>
    <td>年龄</td>
   </tr>
   </thead>
   <c:forEach items="${listusers}" var="u">
    <tr>
     <th>${u.id}</th>
     <th>${u.username}</th>
     <th>${u.age}</th>
    </tr>
    </c:forEach>
  </table>
  <a  class='first'>共${sum}页</a>
  <c:if test="${indexnum!=1}">
 <a href="${pageContext.request.contextPath}/user/test?page=1" class='first'>首页</a>
  <a href="${pageContext.request.contextPath}/user/test?page=${indexnum-1}" class='first'>上一页</a>
 </c:if>
 <span>第${indexnum}页</span>
  <c:if test="${indexnum !=sum}">
 <a href="${pageContext.request.contextPath}/user/test?page=${indexnum+1}" class='first'>下一页</a>
  <a href="${pageContext.request.contextPath}/user/test?page=${sum}" class='first'>尾页</a>
  </c:if>
</div>
</body>

</html>

主要是为了实现功能,页面的样式都没有写,希望能帮到大家。

发布了20 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qrnhhhh/article/details/84826822