分页 检索
controller 页面
检索 名字 deptname
public String list(Model model,String deptname,@RequestParam(defaultValue="1",required=false) Integer pageIndex,@RequestParam(defaultValue=ResourceKeys.DEFAULT_PAGE_SIZE) Integer pageSize){ PageUtil<Dept>pageModel = deptService.getAllDeptWithPages(deptname,pageIndex,pageSize); model.addAttribute("pageModel", pageModel); return “路径” }
xml 页面
<select id="getCategoryCount" parameterType="java.lang.String" resultType="long"> select count(*) from Dept <where> <if test="_parameter!=null"> and deptname like concat ('%',#{_parameter},'%') </if> </where> </select> <select id="getCategoryWithPages" parameterType="java.util.Map" resultType="Dept"> select *,(@rownum:=@rownum+1) i from (SELECT @rownum:=0) r, dept <where> <if test="deptname!=null"> and deptname like concat ('%',#{deptname},'%') </if> </where> limit #{skip},#{take} </select>
serviceImp
@Override public PageUtil<Dept> getAllDeptWithPages(String deptname, Integer pageIndex, Integer pageSize) { // TODO Auto-generated method stub PageUtil<Dept> pageModel =new PageUtil<Dept>(pageIndex,pageSize); Map<String, Object> map = new HashMap<String, Object>(); map.put("deptname",deptname); map.put("skip",(pageIndex-1)*pageSize); map.put("take",pageSize); List<Dept> list = dao.getCategoryWithPages(map); Long count =dao.getCategoryCount(deptname); pageModel.setDateList(list); pageModel.setTotalCount(count); return pageModel; }
jsp 文件写入
<c:forEach items="${pageModel.dateList }" var="emp" varStatus="status">
<tr>
<td>
<input id="id0" type="checkbox" name="chkDeptIds" value="${emp.empid }" /></td>
<td>${(clients.pageIndex-1)*clients.pageSize+status.index+1 }</td>
<td>${emp.empname}</td>
</tr>
</c:forEach>
${(clients.pageIndex-1)*clients.pageSize+status.index+1 } 排列序号
记得 在 jsp 页面 调用时 ${pageModel.dateList }
PageUtil 分页类
package com.wskj.ssm.util; import java.util.List; public class PageUtil<T> { private Integer pageIndex = 1; private Integer pageSize = 10; private Integer pageCount = 0; public PageUtil (){ super(); } public PageUtil(Integer pageIndex, Integer pageSize) { super(); this.pageIndex = pageIndex; this.pageSize = pageSize; } private List<T> dateList; private Long totalCount = 0l; public Integer getPageIndex() { return pageIndex; } public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getPageCount() { pageCount =(int) Math.ceil(this.totalCount * 1.0/this.pageSize); return pageCount; } public void setPageCount(Integer pageCount) { this.pageCount = pageCount; } public List<T> getDateList() { return dateList; } public void setDateList(List<T> dateList) { this.dateList = dateList; } public Long getTotalCount() { return totalCount; } public void setTotalCount(Long totalCount) { this.totalCount = totalCount; } }