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); } }
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);
<%@include file="/WEB-INF/jsp/public/pageView.jspf" %> <s:form action="user_list"></s:form>