Commit dce2849c781749ec6b9a6b4e4318cd3d9da30ece
1 parent
82966b3b
Exists in
master
add UtilRequestMap
Showing
1 changed file
with
157 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,157 @@ | @@ -0,0 +1,157 @@ | ||
| 1 | +package com.taover.util; | ||
| 2 | + | ||
| 3 | +import java.lang.reflect.Field; | ||
| 4 | +import java.util.ArrayList; | ||
| 5 | +import java.util.HashMap; | ||
| 6 | +import java.util.Iterator; | ||
| 7 | +import java.util.List; | ||
| 8 | +import java.util.Map; | ||
| 9 | + | ||
| 10 | +import com.taover.util.UtilLog; | ||
| 11 | +import com.taover.util.UtilString; | ||
| 12 | + | ||
| 13 | +public class UtilHttpRequestMap { | ||
| 14 | + /** | ||
| 15 | + * 将requestMap上的String[]转换为String | ||
| 16 | + * @param requestMap | ||
| 17 | + * @return | ||
| 18 | + */ | ||
| 19 | + public static Map<String, Object> getMapValueStringByRequestMap(Map<String, String[]> requestMap){ | ||
| 20 | + Map<String, Object> result = new HashMap<String, Object>(); | ||
| 21 | + Iterator<String> keyIter = requestMap.keySet().iterator(); | ||
| 22 | + while(keyIter.hasNext()){ | ||
| 23 | + String keyName = keyIter.next(); | ||
| 24 | + result.put(keyName, requestMap.get(keyName)[0]); | ||
| 25 | + } | ||
| 26 | + return result; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * | ||
| 31 | + * @param requestMap | ||
| 32 | + * @param entityClass | ||
| 33 | + * @param exceptColumnList | ||
| 34 | + * @return | ||
| 35 | + */ | ||
| 36 | + public static List<Object[]> getSqlConditionListByRequestMap(Map<String, String[]> requestMap, Class entityClass, String exceptColumnList){ | ||
| 37 | + Iterator<String> keyIter = requestMap.keySet().iterator(); | ||
| 38 | + List<Object[]> result = new ArrayList<Object[]>(); | ||
| 39 | + while (keyIter.hasNext()) { | ||
| 40 | + String keyName = keyIter.next(); | ||
| 41 | + if(exceptColumnList != null && exceptColumnList.contains(keyName)){ | ||
| 42 | + continue; | ||
| 43 | + } | ||
| 44 | + String camelName = UtilString.camelName(keyName); | ||
| 45 | + try { | ||
| 46 | + Field camelField = entityClass.getDeclaredField(camelName); | ||
| 47 | + camelField.setAccessible(true); | ||
| 48 | + result.add(getSqlConditionArrayByStringArray(camelField, keyName, requestMap.get(keyName))); | ||
| 49 | + } catch (Exception e) { | ||
| 50 | + UtilLog.errorForException(e, UtilHttpRequestMap.class); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + return result; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + /** | ||
| 57 | + * 通过Map数据构造bean | ||
| 58 | + * @param dataMap | ||
| 59 | + * @param beanClass | ||
| 60 | + * @param isCopyNull | ||
| 61 | + * @param exceptColumnList | ||
| 62 | + * @return | ||
| 63 | + * @throws Exception | ||
| 64 | + */ | ||
| 65 | + public static <T> T getBeanByMap(Map<String, Object> dataMap, Class<T> beanClass, boolean isCopyNull, String exceptColumnList) throws Exception{ | ||
| 66 | + if(beanClass == null){ | ||
| 67 | + return null; | ||
| 68 | + } | ||
| 69 | + T result = beanClass.newInstance(); | ||
| 70 | + Iterator<String> keyIter = dataMap.keySet().iterator(); | ||
| 71 | + while(keyIter.hasNext()){ | ||
| 72 | + String keyName = keyIter.next(); | ||
| 73 | + String camelName = UtilString.camelName(keyName); | ||
| 74 | + Object keyValue = dataMap.get(keyName); | ||
| 75 | + | ||
| 76 | + if(isCopyNull && keyValue == null){ | ||
| 77 | + continue; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + Field keyField = beanClass.getDeclaredField(camelName); | ||
| 81 | + String keyFieldTypeName = keyField.getType().getSimpleName(); | ||
| 82 | + if(keyValue == null){ | ||
| 83 | + keyField.set(result, null); | ||
| 84 | + }else{ | ||
| 85 | + String keyValueClassName = keyValue.getClass().getSimpleName(); | ||
| 86 | + if(keyValueClassName.equals(keyFieldTypeName)){ | ||
| 87 | + keyField.set(result, keyValue); | ||
| 88 | + }else{ | ||
| 89 | + if(keyFieldTypeName.equals("String")){ | ||
| 90 | + keyField.set(result, keyValue.toString()); | ||
| 91 | + }else if(keyFieldTypeName.equals("Integer")){ | ||
| 92 | + keyField.set(result, Integer.valueOf(keyValue.toString())); | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + return result; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * 通过requestMap的String[]构造Sql Condition条件 | ||
| 102 | + * @param camelField | ||
| 103 | + * @param keyName | ||
| 104 | + * @param data | ||
| 105 | + * @return | ||
| 106 | + * @throws Exception | ||
| 107 | + */ | ||
| 108 | + private static Object[] getSqlConditionArrayByStringArray(Field camelField, String keyName, String[] data) throws Exception{ | ||
| 109 | + if(data == null || data.length == 0){ | ||
| 110 | + throw new Exception("data is null"); | ||
| 111 | + } | ||
| 112 | + String colName = UtilString.underscoreName(keyName).toLowerCase(); | ||
| 113 | + if(data.length == 1){ | ||
| 114 | + String typeName = camelField.getType().getSimpleName(); | ||
| 115 | + if(typeName.equals("String")){ | ||
| 116 | + return new Object[]{colName, "like", "%"+data[0]+"%"}; | ||
| 117 | + }else if(typeName.equals("Date")){ | ||
| 118 | + return new Object[]{"datediff(`"+colName+"`, '"+data+"')=0"}; | ||
| 119 | + }else{ | ||
| 120 | + return new Object[]{colName, "=", data[0]}; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + String sql = colName+" in ('"+data[0]+"'"; | ||
| 124 | + for(int i=1; i<data.length; ++i){ | ||
| 125 | + sql += ", '"+data[i]+"'"; | ||
| 126 | + } | ||
| 127 | + sql += ") "; | ||
| 128 | + return new Object[]{sql}; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * 通过RequestMap获取更新数据的结构数组 | ||
| 133 | + * @param requestMap | ||
| 134 | + * @param entityClass | ||
| 135 | + * @param exceptColumnList | ||
| 136 | + * @return | ||
| 137 | + */ | ||
| 138 | + public static List<Object[]> getUpdateListByRequestMap(Map<String, String[]> requestMap, Class entityClass, String exceptColumnList){ | ||
| 139 | + Iterator<String> keyIter = requestMap.keySet().iterator(); | ||
| 140 | + List<Object[]> result = new ArrayList<Object[]>(); | ||
| 141 | + while (keyIter.hasNext()) { | ||
| 142 | + String keyName = keyIter.next(); | ||
| 143 | + if(exceptColumnList != null && exceptColumnList.contains(keyName)){ | ||
| 144 | + continue; | ||
| 145 | + } | ||
| 146 | + String camelName = UtilString.camelName(keyName); | ||
| 147 | + try { | ||
| 148 | + Field camelField = entityClass.getDeclaredField(camelName); | ||
| 149 | + camelField.setAccessible(true); | ||
| 150 | + result.add(new Object[]{UtilString.underscoreName(keyName).toLowerCase(), requestMap.get(keyName)[0]}); | ||
| 151 | + } catch (Exception e) { | ||
| 152 | + UtilLog.errorForException(e, UtilHttpRequestMap.class); | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + return result; | ||
| 156 | + } | ||
| 157 | +} |