????? 重构一书中有说过,一个方法的参数最好不要超过7个,否则就是不雅的编码。在SWING开发中,表单的提交不同于HTML,并没有请求对象request,控件中的值往往要在提交BUTTON时通过参数的形式传递,如果控件有很多个,传递的值会霸占几个参数位置,当然很多人喜欢用List、Property存储控件值,这里提供另一种思路用于传递控件值到逻辑业务层中,使用中间实体类做传递。
?
1、首先建立中间类,存储表单要提交的参数。
public class Condition implements Serializable{ /** * 逻辑关系(and or !) */ private String logic; /** * */ private String beginBracket; private String fieldName; private String operator; private Object value; private String endBracket; public Condition(String logic, String beginBracket, String fieldName, String operator, Object value, String endBracket){ this.logic = logic; this.beginBracket = beginBracket; this.fieldName = fieldName; this.operator = operator; this.value = value; this.endBracket = endBracket; } /** * @return Returns the beginBracket. */ public String getBeginBracket() { return beginBracket; } /** * @param beginBracket The beginBracket to set. */ public void setBeginBracket(String beginBracket) { this.beginBracket = beginBracket; } /** * @return Returns the endBracket. */ public String getEndBracket() { return endBracket; } /** * @param endBracket The endBracket to set. */ public void setEndBracket(String endBracket) { this.endBracket = endBracket; } /** * @return Returns the fieldName. */ public String getFieldName() { return fieldName; } /** * @param fieldName The fieldName to set. */ public void setFieldName(String fieldName) { this.fieldName = fieldName; } /** * @return Returns the operator. */ public String getOperator() { return operator; } /** * @param operator The operator to set. */ public void setOperator(String operator) { this.operator = operator; } /** * @return Returns the value. */ public Object getValue() { return value; } /** * @param value The value to set. */ public void setValue(Object value) { this.value = value; } /** * @return Returns the logic. */ public String getLogic() { return logic; } /** * @param logic The logic to set. */ public void setLogic(String logic) { this.logic = logic; }}
?
2、表现层将表单中的值插入中间实体中,比如:
List<Condition> conditions = new ArrayList<Condition>();
conditions.add(new Condition("and", null, "billMaster.validDate",">=", CommonVars.getBeginDate(date), null));conditions.add(new Condition("and", null,"billMaster.isValid", "=",new Boolean(true), null));
?
3、
/** * 组织HQL报表条件公共查询 * * @param selects * 选择内容 * @param className * 类名 * @param conditions * 查询条件 * @param groupBy * 分组 * @param leftOuter * 左连接内容 * @return 查询结果 */ public List commonSearch(String selects, String className, List conditions, String groupBy, String leftOuter) { if (leftOuter == null) { leftOuter = ""; } String sql = ""; sql = " from " + className + " a " + leftOuter + " where a.company =? "; if (selects != null && !selects.equals("")) { sql = selects + sql; } else {// selects == null sql = " select a from " + className + " a " + leftOuter + " where a.company =? "; } List<Object> params = new ArrayList<Object>(); params.add(CommonUtils.getCompany()); if (conditions != null) { for (int i = 0; i < conditions.size(); i++) { Condition condition = (Condition) conditions.get(i); if (condition.getLogic() != null) { sql += " " + condition.getLogic() + " "; } if (condition.getBeginBracket() != null) { sql += condition.getBeginBracket(); } if (condition.getFieldName() != null) { sql += "a." + condition.getFieldName(); } if (condition.getOperator() != null) { sql += condition.getOperator(); } if (condition.getValue() != null) { if(condition.getOperator().indexOf("in")>0){ String[] strs = (String[])condition.getValue(); for(int j=0;j<strs.length;j++){ if(j==0){ sql += "?"; }else{ sql +=",?"; } } for(String str : strs){ params.add(str); } }else{ sql += "? "; params.add(condition.getValue()); System.out.println("value1="+condition.getValue()); } } if (condition.getEndBracket() != null) sql += condition.getEndBracket(); } } if (groupBy != null && !groupBy.equals("")) { sql = sql + " " + groupBy; } System.out.println("---------------sql:" + sql); List result = this.find(sql, params.toArray()); return result; }
?
4、在业务逻辑层中调用
?
leftOuter = " left join fetch a.billMaster left join fetch a.wareSet left join fetch a.complex ";List ms = casDao.commonSearch("", billDetailTableName, conditions, "",leftOuter);
?