1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | private static final Map SIMPLE_OPERATOR = new HashMap(); static { SIMPLE_OPERATOR.put(POJOConstant.EQ, ” = “); SIMPLE_OPERATOR.put(POJOConstant.LIKE, ” LIKE “); SIMPLE_OPERATOR.put(POJOConstant.NEQ, ” != “); SIMPLE_OPERATOR.put(POJOConstant.BIGGER_EQ, ” >= “); SIMPLE_OPERATOR.put(POJOConstant.LESS_EQ, ” <= "); SIMPLE_OPERATOR.put(POJOConstant.LESS, ” < "); SIMPLE_OPERATOR.put(POJOConstant.START_WITH, ” LIKE “); SIMPLE_OPERATOR.put(POJOConstant.END_WITH, ” LIKE “); SIMPLE_OPERATOR.put(POJOConstant.BIGGER, ” > “); } /** * 获取高级搜索的where条件 * * @return */ public String getAdvanceSearchSql() { if (extAdvanceFilterParam == null) { return null; } JSONObject extAdvanceFilterParamJson = JSON.parseObject(extAdvanceFilterParam); String filterType = ” OR “; boolean isOr = true; if (extAdvanceFilterParamJson.getString(“filterType”).equals(“and”)) { filterType = ” AND “; isOr = false; } JSONArray extAdvanceFilterParamArray = extAdvanceFilterParamJson.getJSONArray(“extAdvanceFilterParamArray”); JSONObject tempAFilter = null; Field field = null; String sqlField = null; String tempVal = null; StringBuilder whereSql = new StringBuilder(isOr ? ” AND (” : ” AND “); boolean isHashWhere = false; for (int i = 0; i < extAdvanceFilterParamArray.size(); i++) { tempAFilter = extAdvanceFilterParamArray.getJSONObject(i); field = ReflectUtils.getDeclaredField(this.getClass(), tempAFilter.getString(“name”)); if (field == null) { log.error(“字段不存在:” + field); continue; } sqlField = getSqlField(field); tempVal = formartVal(field, tempAFilter.get(“val”), tempAFilter.getString(“filterType”), tempAFilter.getString(“searchKeyType”) , tempAFilter.getString(“fieldName”)); if (sqlField == null || tempVal == null || !SIMPLE_OPERATOR.containsKey(tempAFilter.getString(“filterType”))) { log.error(“条件不满足,无法拼接此字段,详情请打断点:” + field); continue; } if (whereSql.length() > 6) { whereSql.append(filterType); } whereSql.append(sqlField + (“searchKey”.equals(field.getName()) ? ” LIKE ” : SIMPLE_OPERATOR.get(tempAFilter.getString(“filterType”))) + tempVal + ” “); isHashWhere = true; } if(!isHashWhere){ return “”; } whereSql.append(isOr ? “)” : “”); return whereSql.toString(); } /** * 格式化值 * * @param field lambdaSett * @param val值 * @param filterType * @return 值的sql格式 */ protected String formartVal(Field field, Object val, String filterType, String searchKeyType, String fieldName) { if (val == null || “null”.equals(val)) { return “null”; } Class type = field.getType(); String result = null; // 字符串直接是字段名 if (!CheckUtils.isNullOrEmpty(searchKeyType)) { if (“str”.equals(searchKeyType)) { return ” CONCAT(‘%’,'”” + fieldName + “”‘,’%’,'” + val + “‘,’%’) “; } if (“streq”.equals(searchKeyType)) { return ” CONCAT(‘%’,'”” + fieldName + “”:”” + val + “”‘,’%’) “; } else if (“date”.equals(searchKeyType)) { return ” CONCAT(‘%’,'”” + fieldName + “”:”” + val + “‘,’%’) “; } else if (“int”.equals(searchKeyType)) { return ” CONCAT(‘%’,'”” + fieldName + “”:”” + val + “”‘,’%’) “; } } //只有字符串才有like 需要特殊处理 if (type == String.class) { if (POJOConstant.LIKE.equals(filterType)) { result = ” CONCAT(‘%’,'” + val + “‘,’%’) “; } else if (POJOConstant.START_WITH.equals(filterType)) { result = ” CONCAT(‘” + val + “‘,’%’) “; } else if (POJOConstant.END_WITH.equals(filterType)) { result = ” CONCAT(‘%’,'” + val + “‘) “; } else { result = “‘” + val + “‘”; } } else if (type == Number.class || Number.class.isAssignableFrom(type)) { result = ConverterUtils.toString(val); } else if (type == Date.class || Date.class.isAssignableFrom(type)) { Date dateVal = DateUtils.parseStr(ConverterUtils.toString(val)); return “FROM_UNIXTIME(” + (dateVal.getTime() / 1000) + “)”; } else { log.warn(“格式不支持:” + field + val); return null; } return result; } public String getSqlField(Field field) { if (field.isAnnotationPresent(TableField.class)) { TableField tableField = field.getAnnotation(TableField.class); if (tableField.exist()) { return tableField.value(); } else { return null; } } else if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); return column.name(); } return null; } |