一,搜索查询和显示列表都在一个方法里面,只是参数不一样,还是上代码,先说我是如何写的吧
1,有一个分页的包装类,还有一个简单的
package com.zjedusoft.gdqs.common.page; import java.util.HashMap; import java.util.List; import java.util.Map; public class Pagination<T> { private final static Integer DEFAULT_PAGINATION_SIZE = 10; private final static Integer DEFAULT_DISPLAY_PAGE = 1; private Integer totalSize; private Integer totalPages; private Integer maxResult; private Integer skipResult; private Integer currentPage; private List<T> elementList; /** * @return */ private int doRtnDefaultMaxResult() { currentPage = DEFAULT_DISPLAY_PAGE; return DEFAULT_PAGINATION_SIZE; } /** * @return */ private Integer doRtnDefaultSkipResult() { return 0; } /** * @return */ private int doRtnMaxResult() { return (currentPage == totalPages) ? currentPage * DEFAULT_PAGINATION_SIZE : currentPage * DEFAULT_PAGINATION_SIZE; } /** * @return */ private int doRtnSkipResult() { return (currentPage == totalPages) ? (currentPage - 1) * DEFAULT_PAGINATION_SIZE : (currentPage - 1) * DEFAULT_PAGINATION_SIZE; } /** * @return */ private Integer doRtnTotalPages() { return (totalSize % DEFAULT_PAGINATION_SIZE == 0 ? (totalSize / DEFAULT_PAGINATION_SIZE) : (totalSize / DEFAULT_PAGINATION_SIZE + 1)); } /** * @return the currentPage */ public Integer getCurrentPage() { return currentPage; } /** * @return the elementData */ public List<T> getElementList() { return elementList; } /** * 返回一个最大值 */ public Integer getMaxResult() { return isCurrentPageNotEqualDefaultDisplayPage() ? doRtnDefaultMaxResult() : doRtnMaxResult(); } /** * @return Desc: 这个方法的作用主要是拼装sql语句的查询部分, 其中有两项必须填装, 那就是maxResult和skipResult */ public Map<String, Object> getPaginationQryMap() { Map<String, Object> result = new HashMap<String, Object>(); result.put("maxResult", getMaxResult()); result.put("skipResult", getSkipResult()); return result; } /** * 返回的步长 */ public Integer getSkipResult() { return isCurrentPageNotEqualDefaultDisplayPage() ? doRtnDefaultSkipResult() : doRtnSkipResult(); } /** * @return the totalPages */ public Integer getTotalPages() { return (totalSize != null) ? doRtnTotalPages() : 0; } /** * @return the totalSize */ public Integer getTotalSize() { return totalSize; } /** * @return */ private boolean isCurrentPageNotEqualDefaultDisplayPage() { return (currentPage == null || currentPage == DEFAULT_DISPLAY_PAGE); } /** * @param currentPage * the currentPage to set */ public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } /** * @param elementData * the elementData to set */ public void setElementData(List<T> elementList) { this.elementList = elementList; } /** * @param totalPages * the totalPages to set */ public void setTotalPages(Integer totalPages) { this.totalPages = totalPages; } /** * @param totalSize * the totalSize to set */ public void setTotalSize(Integer totalSize) { this.totalSize = totalSize; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Pagination [totalSize=" + totalSize + ", totalPages=" + totalPages + ", maxResult=" + maxResult + ", skipResult=" + skipResult + ", currentPage=" + currentPage + ", elementList=" + elementList + "]"; } }
?
package com.zjedusoft.gdqs.common.page; import java.util.List; public class Page<T> { private int page = 1; private int pageSize = 10; private int totalSize; private int totalPage; private List<T> elementList; public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalSize() { return totalSize; } public void setTotalSize(int totalSize) { this.totalSize = totalSize; this.totalPage = (int) Math.ceil((double) totalSize / pageSize); } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<T> getElementList() { return elementList; } public void setElementList(List<T> elementList) { this.elementList = elementList; } // @Override // public String toString() { // return "{page:" + page + ",pageSize:" + pageSize + ",totalSize:" // + totalSize + ",totalPage:" + totalPage + ",data:" // + elementList + "}"; // } }
?
2,如何使用呢,拿角色管理这个模块来说吧,在action里面,用类来声明一个page参数,查询出来的数据是一个LIST?
private IRoleService roleService; private RoleBean roleBean; private List<RoleBean> roleBeanList; private String keyword=""; private int id; private Pagination<RoleBean> page; public String addRole() { this.roleService.addRole(roleBean); return "success"; } public String deleteRoleManager() { roleService.deleteRoleManager(id); return "success"; } public String getAllRoleManager() { // System.out.println(JSONObject.fromObject(page)); roleBeanList = roleService.getAllRole(doQueByPage(), page) .getElementList(); // System.out.println(JSONArray.fromObject(roleBeanList)); return "success"; } public Map<String, Object> doQueByPage() { Map<String, Object> paramMap = page.getPaginationQryMap(); paramMap.put("maxResult", page.getMaxResult()); paramMap.put("skipResult", page.getSkipResult()); paramMap.put("keyword", "%" + keyword + "%"); return paramMap; }
?
?
3,调用的Service,IService接口就不写了
public Pagination<RoleBean> getAllRole(Map<String, Object> map, final Pagination<RoleBean> page) { page.setElementData(this.roleMapper.getAllRoleByPage(map)); page.setTotalSize(this.roleMapper.listSum(map)); return page; }
?
4,mapper里面
public List<RoleBean> getAllRoleByPage(Map<String, Object> map); public Integer listSum(Map<String, Object> map);
?
5,mapper.xml里面的查询SQL语句
<select id="getAllRoleByPage" resultMap="roleResultMap" parameterType="java.util.Map"> select t.* from ( select temp.* from ( select temp.*, rownum rownum_ from TS_ROLE temp <where> <if test="keyword != '%null%'"> and ROLE_NAME like #{keyword} </if> <if test="keyword != '%null%'"> or ROLE_COMMENTS like #{keyword} </if> </where> order by id ) temp where rownum <= #{maxResult} ) t where rownum_ > #{skipResult} </select>
?
?
<select id="listSum" resultType="java.lang.Integer" > select count(*) from TS_ROLE <where> <if test="keyword != '%%'"> and ROLE_NAME like #{keyword} </if> <if test="keyword != '%%'"> or ROLE_COMMENTS like #{keyword} </if> </where> </select>
?
?
6,这是JSP页面代码
搜素的action和查询列表的action是同一个,就是多带了一个条件而已,呵呵?
function roleSearch(){ var objForm = document.getElementById("roleSearchForm"); objForm.action = "doSearchRole"; objForm.method = "post"; objForm.submit(); } function jumpPage(pageNo, totalPage) { if (pageNo <= 1) { pageNo = 1; } if (pageNo > totalPage) { pageNo = totalPage; } var form = document.getElementById("gridForm"); document.getElementById("pageNo").value = pageNo; form.action = "doGetRoleManager"; form.method = "post"; form.submit(); } function jumpPageTurn(){ var turnPage = document.getElementById("turn").value; jumpPage(turnPage,${page.totalPages}); }
?
?????????
<tfoot> <c:if test="${!empty page.elementList}"> <tr> <th colspan="3">共${page.totalSize}条记录,每页10条</th> <td colspan="3" align="right"> <div class="fr"> <input type="hidden" name="page" value="${page}" /> <input type="hidden" name="pageSize" value="${pageSize}" /> <a href="javascript:jumpPage(1)" class="first_page">首页</a> <a href="javascript:jumpPage(${page.currentPage - 1}, ${page.totalPages})" class="prev_page"> <img src="images/prev_page.png" width="12" height="12" alt="上一页" /> </a> <span class="fl"><span class="red">${page.currentPage}</span>/${page.totalPages}页 </span> <a href="javascript:jumpPage(${page.currentPage + 1}, ${page.totalPages})" class="next_page"> <img src="images/next_page.png" width="12" height="12" alt="下一页" /> </a> <a href="javascript:jumpPage(<fmt:formatNumber type='number' value='${page.totalPages}'/>)" class="last_page">末页</a> <span class="fl"> 转到第</span> <input id="turn" type="text" size="3" class="input turnto_input fl" /> <span class="fl">页 </span> <a href="javascript:jumpPageTurn()"><img src="images/turnto.gif" width="32" height="19" alt="转到" /> </a> </div> </td> </tr> </c:if> </tfoot>
?
?
<s:form id="gridForm" name="gridForm" method="post" autoComplete="off"> <input type="hidden" name="page.currentPage" id="pageNo" value="${page.currentPage}" /> <input type="hidden" name="page.totalPages" value="${page.totalSize}" /> <input type="hidden" value="${keyword}" name="keyword"> </s:form>
?
大致如此吧。代码都在这里了。。希望对有的童鞋有所帮助。。
?