记一次 SSM 分页

1.实体层(entity,pojo,domain)

package com.entity;

import java.io.Serializable;

      private int totalCount;      //总记录数

      private int totalPageCount;  //总页数

      private int startPos;        //开始位置,从0开始

      private boolean hasFrist;    //是否有首页

      private boolean hasPre;      //是否有前一页

      private boolean hasNext;     //是否有后一页

      private boolean hasLast;     //是否有尾页

      /**
* 通过构造函数,传入总记录数和当前页
* @param pageNow
* @param totalCount
*/
public CountPage(int pageNow, int totalCount) {
super();
this.pageNow = pageNow;
this.totalCount = totalCount;
}
//获取总页数
public int getTotalPageCount() {
totalPageCount=getTotalCount()/getPageSize();
return (totalCount/pageSize==1) ? totalPageCount:totalPageCount+1;
} public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
} public int getPageNow() {
return pageNow;
} public void setPageNow(int pageNow) {
this.pageNow = pageNow;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getTotalCount() {
return totalCount;
} public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
//取得选择记录的初始位置
public int getStartPos() {
return (pageNow-1)*pageSize;
} public void setStartPos(int startPos) {
this.startPos = startPos;
}
//判断是否有第一页
public boolean isHasFrist() {
return (pageNow==1) ? false:true;
} public void setHasFrist(boolean hasFrist) {
this.hasFrist = hasFrist;
}
//如果有首页就有前一页
public boolean isHasPre() {
return isHasFrist() ? true:false;
} public void setHasPre(boolean hasPre) {
this.hasPre = hasPre;
}
//如果有尾页就有下一页
public boolean isHasNext() {
return isHasNext() ? true:false;
} public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
//判断是否有尾页
public boolean isHasLast() {
return (pageNow==getTotalCount()) ? false:true;
} public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
} }

2.DAO层

package com.dao;

import java.util.List;

import com.entity.MyAccount;

public interface MyAccountMapper {

    /*  查询所有   传值(当前页,每页显示多少条数据) */
List<MyAccount> findAll(Integer pageNow,Integer pageSize);
/* 查询总数 */
Integer myAccountCount(); }

3.xxxmapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.MyAccountMapper" >
<!-- 查询所有 并用 limit 进行 分页 -->
<select id="findAll" resultType="com.entity.MyAccount">
select * from account limit #{0},#{1}
</select>
<!-- 查询总数 -->
<select id="accountCount" resultType="Integer">
select COUNT(*) from account
</select>
</mapper>

4.服务层(service)

package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.dao.MyAccountMapper;
import com.entity.MyAccount; @Transactional
@Service
public class AccountService { @Autowired
MyAccountMapper accountMapper = null; /**
* 查询 所有 账户
*/
public List<MyAccount> findAll(Integer pageNow,Integer pageSize){
System.out.println("业务查询 >>> 查询我的所有账户");
List<MyAccount> list = accountMapper.findAll(pageNow, pageSize);
System.out.println("查询到的结果是 >>> " + list.toString());
return list;
} /**
* 查询 总页数
*/
public Integer myAccountCount(){
System.out.println("业务查询 >>> 查询所有账户数量");
return accountMapper.myAccountCount();
}
}

5.控制层(Controller)

package com.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.entity.CountPage;
import com.entity.MyAccount;
import com.service.AccountService; @Controller
public class AccountController { @Resource
AccountService accountService = null; /**
* 查询所有账户
*/
@RequestMapping(value = "/account")
public String findAll(Model model,HttpServletRequest request){ //获取当前页数
String pageNow=request.getParameter("pageNow");
//获取总页数
int totalCount=(int)accountService.myAccountCount();
//初始化 CountPage 对象 page
CountPage page=null;
//new 一个 集合 把数据 放进集合里
List<MyAccount> list=new ArrayList<MyAccount>();
if (pageNow!=null) {
page=new CountPage(Integer.parseInt(pageNow), totalCount);
list=this.accountService.findAll(page.getStartPos(),page.getPageSize());
}else {
page=new CountPage(1, totalCount);
list=this.accountService.findAll(page.getStartPos(),page.getPageSize());
}
//把数据放到 model 中并转发到 jsp 页面
model.addAttribute("list", list);
model.addAttribute("page", page);
return "myaccount";
}
}

6.页面代码(JSP)

<div class="pager">
<font size="2">共 ${page.totalPageCount} 页</font> <font size="2">第
${page.pageNow} 页</font> <a href="account?pageNow=1">首页</a>
<c:choose>
<c:when test="${page.pageNow - 1 > 0}">
<a href="account?pageNow=${page.pageNow - 1}">上一页</a>
</c:when>
<c:when test="${page.pageNow - 1 <= 0}">
<a href="account?pageNow=1">上一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="account?pageNow=${page.pageNow}">下一页</a>
</c:when>
<c:when test="${page.pageNow + 1 < page.totalPageCount}">
<a href="account?pageNow=${page.pageNow + 1}">下一页</a>
</c:when>
<c:when test="${page.pageNow + 1 >= page.totalPageCount}">
<a href="account?pageNow=${page.totalPageCount}">下一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="account?pageNow=${page.pageNow}">尾页</a>
</c:when>
<c:otherwise>
<a href="account?pageNow=${page.totalPageCount}">尾页</a>
</c:otherwise>
</c:choose>
</div>

:其中 <a href="account?pageNow=1">首页</a>  ( account  是 控制层(Controller) @RequestMapping(value = "/account") 的值)

上一篇:Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]


下一篇:Ubuntu安装MySQL及使用Xshell连接MySQL出现的问题(2003-Can't connect to MySql server及1045错误)