近期有个动态分页的需求,所以就尝试了一下手写一个动态分页,这里做个简单的记录。
需求
后台动态分页的实现
项目说明
需要一个已经搭建好的能跑的SSM项目
部分代码
Page.java
package com.yzpt.entity.page;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
/**
* Description: 分页处理的bean
*
* @author yzp
*
* @date 2019年10月17日
*
*/
@Setter
@Getter
public class Page<T> implements Serializable{
/**
* serialVersionUID
*
*/
private static final long serialVersionUID = 1L;
// 必要变量
private int pageNo; // 当前页
private int pageSize = 10; // 每页的数量 默认为10 查询数据时的第二个参数
private int size; // 当前页的数量
private int totalCount; // 总记录数
private List<T> resultList; // list结果
// 页面显示变量
private int firstData; // 查询数据时的第一个参数
private int startPage; // 首页
private int endPage; // 末页
private int previousPage; // 上一页
private int nextPage; // 下一页
private int totalPage; // 总页数
public Page() {
}
public Page(int firstData, int size, int totalCount, int pageSize, List<T> resultList) {
this.firstData = firstData;
this.size = size;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.resultList = resultList;
pageNo = (firstData / pageSize) + 1;
totalPage = (totalCount % pageSize) == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
if(totalCount == 0 && size == 0 ) {
pageNo = 1;
totalPage = 1;
}
int pageAreaSize = 4; // 页码显示范围
startPage = (pageNo - pageAreaSize) < 1 ? 1 : (pageNo - pageAreaSize);
endPage = (pageNo + pageAreaSize) > totalPage ? totalPage : (pageNo + pageAreaSize);
previousPage = (pageNo -1) < 1 ? 1 : (pageNo -1);
nextPage = (pageNo + 1) > totalPage ? totalPage : (pageNo + 1);
}
/**
* Description: 获取mysql查询的第一个参数
*
* @param pageNo
* @param pageSize
* @return
*
*/
public static int getFirstData(int pageNo, int pageSize) {
int firstData = (pageNo - 1) * pageSize;
if(firstData < 1)
firstData = 0;
return firstData;
}
}
User.java
package com.yzpt.entity.system;
import lombok.Getter;
import lombok.Setter;
/**
* Description: 用户类
*
* @author yzp
*
* @date 2019年9月25日
*
*/
@Setter
@Getter
public class User {
private Integer id; // 用户id
private String userName; // 用户账号
private String nickName; // 用户昵称
private String password; // 用户密码
private String salt; // 加密密码的盐
private Integer status; // 用户状态,0:创建未认证(比如没有激活,没有输入验证码等) --等待验证的用户,1:正常状态,2:用户被锁定
private String age; // 用户年龄
private String sex; // 用户性别
private String mail; // 用户邮箱
private String photo; // 用户头像
}
UserController.java
package com.yzpt.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yzpt.entity.page.Page;
import com.yzpt.entity.system.User;
import com.yzpt.service.UserService;
/**
* Description: 用户管理控制层
*
* @author yzp
*
* @date 2019年10月14日
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
private static final Logger logger = LogManager.getLogger(UserController.class);
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
/**
* Description: 用户列表查询
*
* @param request
* @param response
* @return
*
*/
@RequestMapping(value = "/userList")
@RequiresPermissions("user:view")
public String userInfo(HttpServletRequest request, HttpServletResponse response) {
String result = "";
try {
int pageNo = request.getParameter("pageNo") != null ? Integer.valueOf(request.getParameter("pageNo")) : 1;
int pageSize = 10;
// 查询所有的用户
Page<User> page = userService.findByPage(pageNo, pageSize);
User currentUser = (User)SecurityUtils.getSubject().getPrincipal();
request.setAttribute("page", page);
request.setAttribute("currentUser", currentUser);
result = "userInfoList";
} catch (Exception e) {
result = "error";
logger.info("用户列表查询失败 ----- "+e);
}
return result;
}
// 用户添加
@RequiresPermissions("userInfo:add")
@RequestMapping("/userAdd")
public String userInfoAdd(){
return "userInfoAdd";
}
// 用户删除
@RequestMapping("/userDel")
@RequiresPermissions("userInfo:del")
public String userDel(){
return "userInfoDel";
}
}
UserServiceImpl.java
package com.yzpt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yzpt.entity.page.Page;
import com.yzpt.entity.system.User;
import com.yzpt.mapper.UserMapper;
import com.yzpt.service.UserService;
/**
* Description: 用户业务实现层
*
* @author yzp
*
* @date 2019年10月14日
*
*/
@Service
public class UserServiceImpl implements UserService {
private UserMapper userMapper;
@Autowired
public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public Page<User> findByPage(int pageNo, int pageSize) {
int totalCount = userMapper.getTotalCount();
int firstData = Page.getFirstData(pageNo, pageSize);
List<User> list = userMapper.findByPage(firstData, pageSize);
int size = list == null ? 0 : list.size();
return new Page<User>(firstData, size, totalCount, pageSize, list);
}
}
UserMapper.xml
<!-- UserMapping -->
<resultMap id="UserResultMap" type="com.yzpt.entity.system.User">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="nickName" column="nick_name"></result>
<result property="password" column="password"></result>
<result property="salt" column="salt"></result>
<result property="state" column="state"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="mail" column="mail"></result>
<result property="photo" column="photo"></result>
</resultMap>
<!-- 获取用户总数量 -->
<select id="getTotalCount" resultType="int">
select count(*) from t_user
</select>
<!-- 分页查询数据 -->
<select id="findByPage" resultMap="UserResultMap">
select * from t_user order by id asc limit #{firstData}, #{pageSize}
</select>
userInfoList.jsp
<div class="pagination">
<div class="myPage header">共${page.totalCount }条记录</div>
<div class="myPage"><a href="user/userList?pageNo=${page.startPage }">首页</a></div>
<div class="myPage"><a href="user/userList?pageNo=${page.previousPage }">上一页</a></div>
<c:forEach begin="${page.startPage }" end="${page.endPage }" var="s">
<c:choose>
<c:when test="${s == page.pageNo }">
<div class="active myPage"><a href="user/userList?pageNo=${s }">${s }</a></div>
</c:when>
<c:otherwise>
<div class="myPage"><a href="user/userList?pageNo=${s }">${s }</a></div>
</c:otherwise>
</c:choose>
</c:forEach>
<div class="myPage"><a href="user/userList?pageNo=${page.nextPage }">下一页</a></div>
<div class="myPage"><a href="user/userList?pageNo=${page.endPage }">末页</a></div>
<div class="myPage">共${page.totalPage }页</div>
</div>
效果图