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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package mobvista.prd.datasource.dao.impl;
import mobvista.prd.datasource.dao.DBCommonDAO;
import mobvista.prd.datasource.db.DBDataSource;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBCommonDAOImpl implements DBCommonDAO {
/**
* 通用查询方法,返回Map集合
* @param sql
* @param params
* @return
* @throws Exception
*/
public List<Map<String, Object>> findMapList(String sql, Object[] params) throws Exception {
return findMapList(DBDataSource.getInstance().getConnection(), sql, params, true);
}
/**
*
* @param conn
* @param sql
* @param params
* @param closeConn
* @return
* @throws Exception
*/
public List<Map<String, Object>> findMapList(Connection conn, String sql, Object[] params, boolean closeConn) throws Exception {
ResultSet rs = null;
PreparedStatement ps = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
Map<String, Object> map = null;
while (rs.next()) {
map = new HashMap<String, Object>();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
map.put(rsmd.getColumnName(i + 1), rs.getObject(i + 1));
}
list.add(map);
}
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (closeConn && conn != null) {
conn.close();
}
}
return list;
}
/**
* 通用查询数据方法,返回clazz类型集合
* @param sql
* @param params
* @param clazz
* @return
* @throws Exception
*/
public <T> List<T> findListByClass(String sql, Object[] params, Class<T> clazz) throws Exception{
ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;
List<T> list = new ArrayList<T>();
try {
conn = DBDataSource.getInstance().getConnection();
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
list = parseResultSet(clazz, rs);
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
/**
* 通用更新方法
* @param sql 更新语句
* @param params 参数
* @return
* @throws Exception
*/
public int update(String sql, Object[] params) throws Exception {
return update(DBDataSource.getInstance().getConnection(), sql, params, true);
}
/**
*
* @param conn
* @param sql
* @param params
* @param closeConn
* @return
* @throws Exception
*/
public int update(Connection conn, String sql, Object[] params, boolean closeConn) throws Exception {
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
int count = ps.executeUpdate();
if (closeConn) {
conn.commit();
}
return count;
} catch (Exception e) {
if (closeConn && conn != null) {
conn.rollback();
}
throw e;
} finally {
if (ps != null) {
ps.close();
}
if (closeConn && conn != null) {
conn.close();
}
}
}
/**
* 通过sql插入数据方法
* @param sql
* @param params
* @return
* @throws Exception
*/
public int insert(String sql, Object[] params) throws Exception {
return insert(DBDataSource.getInstance().getConnection(), sql, params, true);
}
/**
* 通过sql插入数据方法
* @param conn
* @param sql
* @param params
* @return
* @throws Exception
*/
public int insert(Connection conn, String sql, Object[] params, boolean closeConn) throws Exception {
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
int count = ps.executeUpdate();
if (closeConn) {
conn.commit();
}
return count;
} catch (Exception e) {
if (closeConn && conn != null) {
conn.rollback();
}
throw e;
} finally {
if (ps != null) {
ps.close();
}
if (closeConn && conn != null) {
conn.close();
}
}
}
/**
* 解析sql查询结果,并创建clazz类型集合
* @param clazz
* @param rs
* @return
* @throws Exception
*/
private <T> List<T> parseResultSet(Class<T> clazz, ResultSet rs) throws Exception {
Class.forName(clazz.getName());
Field[] fields = clazz.getDeclaredFields();
T instance = null;
@SuppressWarnings("rawtypes")
Class type = null;
String name = null;
Method method = null;
String methodName = null;
List<T> list = new ArrayList<T>();
while (rs.next()) {
instance = clazz.newInstance();
for (Field field : fields) {
name = field.getName();
if (!"serialVersionUID".equals(name) && !"table".equals(name)) {
methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
type = field.getType();
method = clazz.getMethod(methodName, type);
if (type == Integer.class) {
method.invoke(instance, rs.getInt(name));
} else if (type == String.class) {
method.invoke(instance, rs.getString(name));
} else if (type == Long.class) {
method.invoke(instance, rs.getLong(name));
} else if (type == Double.class) {
method.invoke(instance, rs.getDouble(name));
} else if (type == Float.class) {
method.invoke(instance, rs.getFloat(name));
} else if (type == Boolean.class) {
method.invoke(instance, rs.getBoolean(name));
} else if (type == BigDecimal.class) {
method.invoke(instance, rs.getBigDecimal(name));
} else if (type == Blob.class) {
method.invoke(instance, rs.getBlob(name));
} else if (type == Clob.class) {
method.invoke(instance, rs.getClob(name));
} else if (type == Byte.class) {
method.invoke(instance, rs.getByte(name));
} else if (type == Date.class) {
if (rs.getDate(name) != null) {
method.invoke(instance, new Date(rs.getDate(name).getTime()));
}
} else if (type == Short.class) {
method.invoke(instance, rs.getShort(name));
}
}
}
list.add(instance);
}
return list;
}
/**
* 通用新增方法
* @param obj
* @return
* @throws Exception
*/
public Integer persist(Serializable obj) throws Exception {
Integer id = null;
ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBDataSource.getInstance().getConnection();
conn.setAutoCommit(false);
Map<String, Object> map = buildSqlAndParams(obj);
String sql = (String) map.get("sql");
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
@SuppressWarnings("unchecked")
List<Object> params = (List<Object>) map.get("params");
if (params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
ps.setObject(i + 1, params.get(i));
}
}
ps.execute();
rs = ps.getGeneratedKeys();
while (rs.next()) {
id = rs.getInt(1);
}
conn.commit();
} catch (Exception e) {
if (conn != null) {
conn.rollback();
}
throw e;
} finally {
if(rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
return id;
}
private Map<String, Object> buildSqlAndParams (Serializable obj) throws Exception {
Class<?> clazz = obj.getClass();
Class.forName(clazz.getName());
StringBuffer columns = new StringBuffer("(");
StringBuffer values = new StringBuffer("(");
List<Object> params = new ArrayList<Object>();
String name = null;
Object value = null;
Method method = null;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
name = field.getName();
if (!"serialVersionUID".equals(name) && !"table".equals(name)) {
method = obj.getClass().getDeclaredMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1), null);
value = method.invoke(obj, null);
params.add(value);
columns.append(name).append(",");
values.append("?,");
}
}
if (columns.length() > 1) {
columns.setLength(columns.length() - 1);
values.setLength(values.length() - 1);
}
columns.append(")");
values.append(")");
Field tableField = clazz.getDeclaredField("table");
tableField.setAccessible(true);
Object temp = tableField.get(obj);
if (temp == null) {
throw new Exception(clazz.getName() + " 缺少table成员变量 ");
}
String table = String.valueOf(temp);
StringBuffer sql = new StringBuffer("insert into ").append(table);
sql.append(columns).append(" values ").append(values);
Map<String, Object> map = new HashMap<String, Object>();
map.put("sql", sql.toString());
map.put("params", params);
return map;
}
}