/** * 查询空值或者不为空值的情况 setNullFieldQuery * * @param queryType 参数说明 * @param filedName 字段名 */ public void setNullOrNotNullFieldQuery(String queryType, String... filedName) { if (StringUtils.isNotBlank(queryType) && !JFinalExtKit.isNullOrEmpty(filedName)) { if (!QueryType.NOT_EMPTY.equals(queryType) && !QueryType.EMPTY.equals(queryType)) { log.error("空值或者非空查询的类型只能为:EMPTY、NOT_EMPTY"); throw new RuntimeException("空值或者非空查询的类型只能为:EMPTY、NOT_EMPTY"); } Map<String, Object[]> map = conditionMap.get(); map.put(queryType, filedName); conditionMap.set(map); } }
/** * ************************************************************************* 构建SQL语句 * * @param sb 用于拼接SQL语句 * @param queryType 查询类型 * @param fieldName 字段名称 * @param fieldValue 字段值 * @param alias 别名 */ @SuppressWarnings("rawtypes") private void buildSQL( StringBuilder sb, String queryType, String fieldName, Object fieldValue, String alias, ArrayList<Object> params) { // 非空的时候进行设置 if (!JFinalExtKit.isNullOrEmpty(fieldValue) && !JFinalExtKit.isNullOrEmpty(fieldName)) { if (QueryType.EQUAL.equals(queryType)) { sb.append(" and " + alias + fieldName + " = ? "); params.add(fieldValue); } else if (QueryType.NOT_EQUAL.equals(queryType)) { sb.append(" and " + alias + fieldName + " <> ? "); params.add(fieldValue); } else if (QueryType.LESS_THEN.equals(queryType)) { sb.append(" and " + alias + fieldName + " < ? "); params.add(fieldValue); } else if (QueryType.LESS_EQUAL.equals(queryType)) { sb.append(" and " + alias + fieldName + " <= ? "); params.add(fieldValue); } else if (QueryType.GREATER_THEN.equals(queryType)) { sb.append(" and " + alias + fieldName + " > ? "); params.add(fieldValue); } else if (QueryType.GREATER_EQUAL.equals(queryType)) { sb.append(" and " + alias + fieldName + " >= ? "); params.add(fieldValue); } else if (QueryType.FUZZY.equals(queryType)) { sb.append(" and " + alias + fieldName + " like ? "); params.add("%" + fieldValue + "%"); } else if (QueryType.FUZZY_LEFT.equals(queryType)) { sb.append(" and " + alias + fieldName + " like ? "); params.add("%" + fieldValue); } else if (QueryType.FUZZY_RIGHT.equals(queryType)) { sb.append(" and " + alias + fieldName + " like ? "); params.add(fieldValue + "%"); } else if (QueryType.IN.equals(queryType)) { try { List list = (List) fieldValue; StringBuffer instr = new StringBuffer(); sb.append(" and " + alias + fieldName + " in ("); for (Object obj : list) { instr.append(StringUtils.isNotBlank(instr) ? ",?" : "?"); params.add(obj); } sb.append(instr + ") "); } catch (Exception e) { throw new RuntimeException( "使用IN条件的时候传入的值必须是个List对象,否则将会转换出错!例如将 in('1','2','3')中的'1','2','3'分为三个分别添加到List中做为值传入.", e); } } else if (QueryType.NOT_IN.equals(queryType)) { try { List list = (List) fieldValue; StringBuffer instr = new StringBuffer(); sb.append(" and " + alias + fieldName + " not in ("); for (Object obj : list) { instr.append(StringUtils.isNotBlank(instr) ? ",?" : "?"); params.add(obj); } sb.append(instr + ") "); } catch (Exception e) { throw new RuntimeException( "使用NOT IN条件的时候传入的值必须是个List对象,否则将会转换出错!例如将 not in('1','2','3')中的'1','2','3'分为三个分别添加到List中做为值传入.", e); } } } else { if (QueryType.EMPTY.equals(queryType)) { sb.append(" and " + alias + fieldName + " is null "); } else if (QueryType.NOT_EMPTY.equals(queryType)) { sb.append(" and " + alias + fieldName + " is not null "); } } }
/** * ************************************************************************* 构建条件语句 * * @param alias 别名 * @param fieldNames 所有查询的字段名称 * @param valueMap 所有的值的map */ private void buildCondition(String alias, String[] fieldNames, Map<String, Object> valueMap) { try { // 构建条件前先清空变量 sql.set(""); paramList.set(new ArrayList<Object>()); // 用于存放参数列表 ArrayList<Object> paramArrayList = new ArrayList<Object>(); StringBuilder sb = new StringBuilder(); // 所有的字段名称 Map<String, String> usedFieldMap = new HashMap<String, String>(); if (!conditionMap.get().isEmpty()) { for (Entry<String, Object[]> map : conditionMap.get().entrySet()) { String queryType = map.getKey(); Object[] array = map.getValue(); if (queryType.indexOf("#") > 0) { // 传值查询 String fieldQueryType = queryType.split("#")[0]; String fieldName = array[0] != null ? array[0].toString() : ""; Object fieldValue = array[1]; // 将设置过的字段保存到数组中 usedFieldMap.put(fieldName, fieldName); // 构建SQL语句 buildSQL(sb, fieldQueryType, fieldName, fieldValue, alias, paramArrayList); } else { // 字段查询 if (!"GLOBALTYPE".equals(queryType)) { for (Object field : array) { String filedName = field != null ? field.toString() : ""; if (!excludeFieldMap.get().containsKey(filedName)) { Object fieldValue = valueMap.get(filedName); if (QueryType.EMPTY.equals(queryType) || QueryType.NOT_EMPTY.equals(queryType)) { fieldValue = null; } // 将设置过的字段保存到数组中 usedFieldMap.put(filedName, filedName); // 构建查询语句 buildSQL(sb, queryType, filedName, fieldValue, alias, paramArrayList); } } } } } } // 对没有设置条件的字段进行查询类型设置 String queryType = QueryType.EQUAL; if (conditionMap.get().containsKey("GLOBALTYPE")) { String[] typeArray = (String[]) conditionMap.get().get("GLOBALTYPE"); queryType = typeArray[0]; } // 对未使用过的字段进行build for (String field : fieldNames) { if (!usedFieldMap.containsKey(field)) { Object fieldValue = valueMap.get(field); // 构建查询语句 buildSQL(sb, queryType, field, fieldValue, alias, paramArrayList); } } // 合并传入的参数到参数对象中 sql.set(sb.toString()); paramList.set(paramArrayList); conditionMap.set(new HashMap<String, Object[]>()); // 清空本次的条件map excludeFieldMap.set(new HashMap<String, String>()); // 清空本次的排除字段 } catch (Exception e) { log.error("Conditions构建SQL语句出现错误,请仔细检查!", e); e.printStackTrace(); } }