SSM框架中的分页操作

来源:互联网 发布:半身裙淘宝店好一点 编辑:程序博客网 时间:2024/05/20 08:23

在有分页需求时,主要是要想到对分页的封装。

这里的需求是按姓名或部门对员工信息进行查询,并对查询结果进行分页。


一、对Page分页进行封装

package cn.ssm.po;import java.io.Serializable;import java.util.List;public class PageBean<T> implements Serializable{private int currPage;//当前页数    private int pageSize;//每页显示的记录数    private int totalCount;//总记录数    private int totalPage;//总页数    private List<T> lists;//每页的显示的数据public int getCurrPage() {return currPage;}public void setCurrPage(int currPage) {this.currPage = currPage;}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 getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public List<T> getLists() {return lists;}public void setLists(List<T> lists) {this.lists = lists;}}

二、业务层service

对PageBean进行初始化,并向持久层提出分页请求,以及返回一个当前页式数据的list

public PageBean<PersonDept> findByPage(int currentPage,String name,String deptno){HashMap<String,Object> map = new HashMap<String,Object>();PageBean<PersonDept> pageBean = new PageBean<PersonDept>();    //封装当前页数        pageBean.setCurrPage(currentPage);        //每页显示的数据int pageSize=5;pageBean.setPageSize(pageSize);PersonDept person=new PersonDept();person.setName(name);person.setDeptno(deptno);//封装总记录数int totalCount = personMapperLogin.selectCount(person);pageBean.setTotalCount(totalCount);//封装总页数double tc = totalCount;        Double num =Math.ceil(tc/pageSize);//向上取整        pageBean.setTotalPage(num.intValue());      map.put("start",(currentPage-1)*pageSize);map.put("size", pageBean.getPageSize());map.put("name", name);map.put("deptno", deptno);//封装每页显示的数据List<PersonDept> lists = personMapperLogin.findByPage(map);pageBean.setLists(lists);return pageBean;}
三、持久层的操作

主要运用sql语句的limit

<!-- 根据分页数据start 和size查询数据 --><select id="findByPage" parameterType="Map" resultMap="BaseResultMap">selectperson.NO,person.Name,person.Sex,person.Birthday,person.Professor,person.password,Dept.DeptNamefrom person ,dept <where>person.DeptNo=dept.DeptNo   <include refid="query_person_where"></include>  </where><if test="start!=null and size!=null">limit #{start},#{size}</if></select>

四、控制层Controller的操作

@RequestMapping("/queryPerson1")public String queryPerson1(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model,@RequestParam(value="name",defaultValue="",required=false)String name,@RequestParam(value="deptno",defaultValue="",required=false)String deptno){model.addAttribute("pagemsg", userService.findByPage(currentPage,name,deptno));//回显分页数据model.addAttribute("name", name);model.addAttribute("selectedno", deptno);return "personlist";}

五、Jsp页面的操作

<tr align="center"><td class="td2" align="center" colspan="8">   <span>第${requestScope.pagemsg.currPage }/ ${requestScope.pagemsg.totalPage}页</span>     <span>总记录数:${requestScope.pagemsg.totalCount }  每页显示:${requestScope.pagemsg.pageSize}</span>     <span>       <c:if test="${requestScope.pagemsg.currPage != 1}">           <a  href="${pageContext.request.contextPath }/queryPerson1?currentPage=1&name=${name }&deptno=${selectedno }">[首页]</a>             <a  href="${pageContext.request.contextPath }/queryPerson1?currentPage=${requestScope.pagemsg.currPage-1}&name=${name }&deptno=${selectedno }">[上一页]</a>         </c:if>              <c:if test="${requestScope.pagemsg.currPage != requestScope.pagemsg.totalPage}">           <a  href="${pageContext.request.contextPath }/queryPerson1?currentPage=${requestScope.pagemsg.currPage+1}&name=${name }&deptno=${selectedno}">[下一页]</a>             <a  href="${pageContext.request.contextPath }/queryPerson1?currentPage=${requestScope.pagemsg.totalPage}&name=${name }&deptno=${selectedno }">[尾页]</a>         </c:if>   </span></td></tr>

六、结果




原创粉丝点击