본문 바로가기
Developer/Java

[Java] DataMap

by 순수한소년 2021. 11. 17.
728x90
반응형

@

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package test.cmm.util;
 
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import egovframework.com.utl.fcc.service.EgovStringUtil;
import egovframework.rte.psl.dataaccess.util.CamelUtil;
 
/**
 * @Class Name : DataMap.java
 * @version 1.0
 * DB 리턴 받을때 사용하거나 Map 타입으로 사용할때 사용한다.
 */
public class DataMap extends LinkedHashMap<String, Object> implements Map<String, Object> {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataMap.class);
    private static final long serialVersionUID = 1L;
 
    public static DataMap getNewDataMap() {
        return new DataMap();
    }
 
    public static DataMap getNewDataMap(HttpServletRequest request) {
 
        DataMap map = new DataMap();
        map.defaultSetInfo(request, map);
 
        return map;
    }
 
    public void setResult(String key, DataMap map, List<DataMap> list) {
        if (map != null) {
            map.put(key, list);
        }
    }
 
    @SuppressWarnings("unchecked")
    public List<DataMap> getResult(String key, DataMap map) {
        List<DataMap> list = new ArrayList<DataMap>();
        if (map != null && map.get(key) != null) {
            if (map.get(key) instanceof ArrayList) {
                list = (List<DataMap>) map.get(key);
            }
        }
        return list;
    }
 
    public void setResult(String key, List<DataMap> list) {
        this.put(key, list);
    }
 
    @SuppressWarnings("unchecked")
    public List<DataMap> getResult(String key) {
        List<DataMap> list = new ArrayList<DataMap>();
        if (this.get(key) != null) {
            if (this.get(key) instanceof ArrayList) {
                list = (List<DataMap>this.get(key);
            }
        }
        return list;
    }
 
    private static DataMap makePrameterMap(HttpServletRequest request) {
 
        String reqURI = request.getRequestURI().toString();
        System.out.println("\n# DataMap makePrameterMap 시작---------------------------------------------------------------");
        System.out.println(reqURI);
 
        DataMap resultMap = new DataMap();
 
        int cnt = 0;
        Enumeration<?> reqParams = request.getParameterNames();
        while (reqParams.hasMoreElements()) {
            String keyName = (String) reqParams.nextElement();
            System.out.println("#    DataMap Parameter" + cnt + " # " + keyName + " : [" + request.getParameter(keyName) + "]");
            resultMap.put(keyName, request.getParameter(EgovStringUtil.nullConvert(keyName)));
            cnt++;
        }
 
        defaultSetInfo(request, resultMap);
        System.out.println("# DataMap makePrameterMap 종료---------------------------------------------------------------\n");
 
        return (DataMap) resultMap;
    }
 
    private static void defaultSetInfo(HttpServletRequest request, Map<String, Object> resultMap) {
        resultMap.put("contentType", request.getContentType());
        resultMap.put("contextPath", request.getContextPath());
        resultMap.put("uri", request.getRequestURI());
        resultMap.put("protocol", request.getProtocol());
        resultMap.put("ip", request.getRemoteAddr());
    }
 
    public static DataMap getParamMap(HttpServletRequest request) {
        return makePrameterMap(request);
    }
 
    private static String nvlTrim(String obj) {
        String result = "";
        if (obj != null)
            result = obj.trim();
        return result;
    }
 
    public Object get(String key) {
        Object o = null;
        try {
            o = super.get(key);
        } catch (NullPointerException e) {
            LOGGER.error("Error in get() key=" + key, e);
        }
        return o;
    }
 
    public String[] getStringArray(String key) {
        String[] r = null;
        try {
            if (super.get(key) instanceof String[]) {
                r = (String[]) super.get(key);
            } else {
                r = new String[1];
                r[0= getString(key);
            }
        } catch (NullPointerException e) {
            LOGGER.error("Error in getStringArray() key=" + key, e);
        }
        return r;
    }
 
    public String getString(String key) {
        String r = "";
        try {
            if (super.get(key) != null) {
                r = super.get(key).toString();
            }
        } catch (NullPointerException e) {
            LOGGER.error("Error in getString() key=" + key, e);
        }
        return r;
    }
 
    public int getInt(String key) {
        return getInt(key, 0);
    }
 
    public int getInt(String key, int defaultInt) {
        int result = 0;
        String str = getString(key);
        try {
            if (StringUtils.isNotEmpty(str)) {
                result = new Integer(str).intValue();
            }
        } catch (NumberFormatException e) {
            LOGGER.error("Error in getInt() key=" + key + ", getString(key)==" + getString(key), e);
            result = defaultInt;
        }
        return result;
    }
 
    public long getLong(String key) {
        return getLong(key, 0);
    }
 
    public long getLong(String key, long defaultLong) {
        long result = 0;
        String str = getString(key);
        try {
            if (StringUtils.isNotEmpty(str)) {
                result = new Long(str).longValue();
            }
        } catch (NumberFormatException e) {
            LOGGER.error("Error in getLong() key=" + key + ", getString(key)==" + getString(key), e);
            result = defaultLong;
        }
        return result;
    }
 
    public double getDouble(String key) {
        return getDouble(key, 0);
    }
 
    public double getDouble(String key, double defaultDouble) {
        double result = 0;
        String str = getString(key);
        try {
            if (StringUtils.isNotEmpty(str)) {
                result = new Double(str).doubleValue();
            }
        } catch (NumberFormatException e) {
            LOGGER.error("Error in getDouble() key=" + key + ", getString(key)=" + getString(key), e);
            result = defaultDouble;
        }
        return result;
    }
 
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }
 
    public boolean getBoolean(String key, boolean defaltValue) {
        boolean result = defaltValue;
        String strObj = nvlTrim(key);
        if (strObj.length() > 0) {
            result = strObj.toLowerCase().equals("true");
        }
        return result;
    }
 
    public void setString(String key, String value) {
        super.put(key, value);
    }
 
    public void setInt(String key, int value) {
        super.put(key, value);
    }
 
    public void setInt(String key, String value) {
        super.put(key, new Integer(value).intValue());
    }
 
    public void setLong(String key, long value) {
        super.put(key, value);
    }
 
    public void setLong(String key, String value) {
        super.put(key, new Long(value).longValue());
    }
 
    public void setDouble(String key, double value) {
        super.put(key, value);
    }
 
    public void setDouble(String key, String value) {
        super.put(key, new Double(value).doubleValue());
    }
 
    public void setBoolean(String key, boolean value) {
        super.put(key, new Boolean(value));
    }
 
    public Object put(String key, Object value) {
        return super.put(CamelUtil.convert2CamelCase((String) key.trim()), value);
    }
}
 
cs

@

1
2
3
4
5
6
//리퀘스트 맵으로 넣기
DataMap reqMap = DataMap.getParamMap(request);
 
//SQL파라미터로 활용
DataMap param  = new DataMap();
param.put(key, list);
cs

@

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mapper-config.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 
    <!--Mybatis 설정 -->
    <settings>
        <!-- 전통적인 데이터베이스 컬럼명 형태인 A_COLUMN을 CamelCase형태의 자바 프로퍼티명 형태인 aColumn으로 자동으로 매핑하도록 함 -->
        <setting name="mapUnderscoreToCamelCase" value="true"></setting>
        <!--  파라미터에 Null 값이 있을 경우 에러 처리 -->
        <setting name="jdbcTypeForNull" value="VARCHAR"></setting>
    </settings>
    
    <!-- Type Aliases 설정-->
    <typeAliases>
        <typeAlias alias="dataMap"             type="test.cmm.util.DataMap" />
        <typeAlias alias="egovMap"             type="egovframework.rte.psl.dataaccess.util.EgovMap" />
        <typeAlias alias="loginVO"             type="test.lgn.service.LoginVO" />                
    </typeAliases>
    
</configuration>
cs

@

728x90
반응형