分頁功能的實現(架構式)

來源:互聯網
上載者:User

1、QueryHelper

package com.njupt.util;import java.util.ArrayList;import java.util.List;import com.njupt.base.BaseDao;import com.njupt.pojo.PageBean;import com.opensymphony.xwork2.ActionContext;/** * 用於輔助拼接HQL語句 *  *  *  */public class QueryHelper {private String fromClause; // FROM子句private String whereClause = ""; // Where子句private String orderByClause = ""; // OrderBy子句private List<Object> parameters = new ArrayList<Object>(); // 參數列表/** * 產生From子句 *  * @param clazz * @param alias *            別名 */public QueryHelper(Class clazz, String alias) {fromClause = "FROM " + clazz.getSimpleName() + " " + alias;}/** * 拼接Where子句 *  * @param condition * @param params */public QueryHelper addCondition(String condition, Object... params) {// 拼接if (whereClause.length() == 0) {whereClause = " WHERE " + condition;} else {whereClause += " AND " + condition;}// 參數if (params != null) {for (Object p : params) {parameters.add(p);}}return this;}/** * 如果第一個參數為true,則拼接Where子句 *  * @param append * @param condition * @param params */public QueryHelper addCondition(boolean append, String condition, Object... params) {if (append) {addCondition(condition, params);}return this;}/** * 拼接OrderBy子句 *  * @param propertyName *            參與排序的屬性名稱 * @param asc *            true表示升序,false表示降序 */public QueryHelper addOrderProperty(String propertyName, boolean asc) {if (orderByClause.length() == 0) {orderByClause = " ORDER BY " + propertyName + (asc ? " ASC" : " DESC");} else {orderByClause += ", " + propertyName + (asc ? " ASC" : " DESC");}return this;}/** * 如果第一個參數為true,則拼接OrderBy子句 *  * @param append * @param propertyName * @param asc */public QueryHelper addOrderProperty(boolean append, String propertyName, boolean asc) {if (append) {addOrderProperty(propertyName, asc);}return this;}/** * 擷取產生的用於查詢資料列表的HQL語句 *  * @return */public String getListQueryHql() {return fromClause + whereClause + orderByClause;}/** * 擷取產生的用於查詢總記錄數的HQL語句 *  * @return */public String getCountQueryHql() {return "SELECT COUNT(*) " + fromClause + whereClause;}/** * 擷取HQL中的參數值列表 *  * @return */public List<Object> getParameters() {return parameters;}/** * 查詢分頁資訊,並放到值棧棧頂 *  * @param service * @param pageNum * @param pageSize */public void preparePageBean(BaseDao<?> service, int pageNum, int pageSize) {PageBean pageBean = service.getPageBean(pageNum, pageSize, this);ActionContext.getContext().getValueStack().push(pageBean);}}

2、在Action的相應部位加上類十餘以下的代碼

new QueryHelper(Topic.class, "t")//// 過濾條件.addCondition("t.forum=?", forum)//.addCondition((viewType == 1), "t.type=?", Topic.TYPE_BEST) // 1 表示只看精華帖// 排序條件.addOrderProperty((orderBy == 1), "t.lastUpdateTime", asc) // 1 表示只按最後更新時間排序.addOrderProperty((orderBy == 2), "t.postTime", asc) // 2 表示只按主題發表時間排序.addOrderProperty((orderBy == 3), "t.replyCount", asc) // 3 表示只按回複數量排序.addOrderProperty((orderBy == 0), "(CASE t.type WHEN 2 THEN 2 ELSE 0 END)", false)//.addOrderProperty((orderBy == 0), "t.lastUpdateTime", false) // 0 表示預設排序(所有置頂帖在前面,並按最後更新時間降序排列).preparePageBean(topicService, pageNum, pageSize);

3、在JSP頁面中加上以下代碼

 <%@include file="/WEB-INF/jsp/public/pageView.jspf" %>        <s:form action="user_list"></s:form>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.