SSM framework to achieve paging function

It belongs to a good memory as bad written series, his usual little things recorded, to record today about how to implement paging functionality.

First, use the SSM framework, SSM framework used, or more. So I chose to still use SSM framework,

To implement paging function is very simple, sql is also very simple, is select * from table limit start end; (mysql database use)

First, there must be a page can be a table tag

First need to query the database, the library needs to show a total of how many. select count (*) from table;

controller layer

@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 <the User> Users = userservice.querylist (Map); // query a list of all users, using the list received
  model.addAttribute ( "listusers", users ); // modle transmit data to the JSP
  model.addAttribute ( "SUM", sumcount);
  model.addAttribute ( "indexnum", indexnum);
  return "Login";
 }

Service Layer

I put business logic written in the controller layer, the service layer is the corresponding interfaces and methods, methods dao layer called serviceImpl in. Then the corresponding Mapper in the 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

<%@ 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>

 

Mainly in order to realize the function, style pages are not written, hope to help you.

 

 

 

 

Published 20 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qrnhhhh/article/details/84826822