| |
1
| +++ a/src/main/java/com/taover/repository/CustomJdbcTemplate.java |
| @@ -0,0 +1,703 @@ |
| @@ -0,0 +1,703 @@ |
| |
1
| +package com.taover.repository; |
| |
2
| + |
| |
3
| +import java.io.Serializable; |
| |
4
| +import java.lang.reflect.Field; |
| |
5
| +import java.lang.reflect.ParameterizedType; |
| |
6
| +import java.math.BigInteger; |
| |
7
| +import java.util.ArrayList; |
| |
8
| +import java.util.HashMap; |
| |
9
| +import java.util.Iterator; |
| |
10
| +import java.util.List; |
| |
11
| +import java.util.Map; |
| |
12
| + |
| |
13
| +import javax.annotation.Resource; |
| |
14
| +import javax.persistence.Column; |
| |
15
| +import javax.persistence.Id; |
| |
16
| +import javax.persistence.Table; |
| |
17
| + |
| |
18
| +import org.springframework.jdbc.core.JdbcTemplate; |
| |
19
| +import org.springframework.util.StringUtils; |
| |
20
| + |
| |
21
| +/** |
| |
22
| + * |
| |
23
| + * @author root |
| |
24
| + * |
| |
25
| + * @param <T> |
| |
26
| + * @param <ID> |
| |
27
| + */ |
| |
28
| +public class CustomJdbcTemplate<T, ID extends Serializable> { |
| |
29
| + @Resource |
| |
30
| + private JdbcTemplate jdbcTemplateRead; |
| |
31
| + @Resource |
| |
32
| + private JdbcTemplate jdbcTemplateWrite; |
| |
33
| + |
| |
34
| + private Map<String, String> beanToTableField; |
| |
35
| + private Map<String, String> tableToBeanField; |
| |
36
| + private String tableFieldNameListGapWithComma; |
| |
37
| + private String idTableFieldName; |
| |
38
| + private String idBeanFieldName; |
| |
39
| + private String dbName; |
| |
40
| + private String tableName; |
| |
41
| + private Class<T> tClassInfo; |
| |
42
| + private CustomJdbcTemplateRowMapper customJdbcTemplateRowMapper; |
| |
43
| + |
| |
44
| + public CustomJdbcTemplateRowMapper getCustomJdbcTemplateRowMapper(){ |
| |
45
| + return this.customJdbcTemplateRowMapper; |
| |
46
| + } |
| |
47
| + |
| |
48
| + public CustomJdbcTemplate() throws Exception{ |
| |
49
| + //获取泛型类Class |
| |
50
| + this.tClassInfo = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
| |
51
| + |
| |
52
| + //检查实体声明 |
| |
53
| + Table annoTable = (Table) tClassInfo.getAnnotation(Table.class); |
| |
54
| + if(annoTable == null){ |
| |
55
| + throw new Exception("DAO层初始化失败,失败原因:"+tClassInfo.getName()+"实体类,没有@Table注解指定表名"); |
| |
56
| + } |
| |
57
| + this.tableName = annoTable.name(); |
| |
58
| + if(annoTable.schema() != null){ |
| |
59
| + this.dbName = annoTable.schema(); |
| |
60
| + }else if(annoTable.catalog() != null){ |
| |
61
| + this.dbName = annoTable.catalog(); |
| |
62
| + } |
| |
63
| + |
| |
64
| + //初始化数据 |
| |
65
| + beanToTableField = new HashMap<String, String>(); |
| |
66
| + tableToBeanField = new HashMap<String, String>(); |
| |
67
| + tableFieldNameListGapWithComma = ""; |
| |
68
| + Field[] declaredFields = tClassInfo.getDeclaredFields(); |
| |
69
| + for(int i=0; i<declaredFields.length; ++i){ |
| |
70
| + Field currField = declaredFields[i]; |
| |
71
| + String fieldName = currField.getName(); |
| |
72
| + Id annoId = (Id)currField.getAnnotation(Id.class); |
| |
73
| + Column annoColumn = (Column)currField.getAnnotation(Column.class); |
| |
74
| + if(annoId != null){ |
| |
75
| + if(annoColumn == null){ |
| |
76
| + idTableFieldName = this.camelToUnderline(fieldName); |
| |
77
| + }else{ |
| |
78
| + idTableFieldName = annoColumn.name(); |
| |
79
| + } |
| |
80
| + idBeanFieldName = fieldName; |
| |
81
| + tableFieldNameListGapWithComma += idTableFieldName+","; |
| |
82
| + beanToTableField.put(fieldName, idTableFieldName); |
| |
83
| + tableToBeanField.put(idTableFieldName, fieldName); |
| |
84
| + }else{ |
| |
85
| + if(annoColumn != null){ |
| |
86
| + String tableFieldName = annoColumn.name(); |
| |
87
| + tableFieldNameListGapWithComma += tableFieldName+","; |
| |
88
| + beanToTableField.put(fieldName, tableFieldName); |
| |
89
| + tableToBeanField.put(tableFieldName, fieldName); |
| |
90
| + } |
| |
91
| + } |
| |
92
| + } |
| |
93
| + |
| |
94
| + //检验是否有属性 |
| |
95
| + if(beanToTableField.isEmpty()){ |
| |
96
| + throw new Exception("DAO层初始化失败,失败原因:"+this.tClassInfo.getName()+"实体类,没有在实体中找到属性信息"); |
| |
97
| + } |
| |
98
| + |
| |
99
| + //去除逗号 |
| |
100
| + tableFieldNameListGapWithComma = tableFieldNameListGapWithComma.substring(0, tableFieldNameListGapWithComma.length()-1); |
| |
101
| + |
| |
102
| + //创建rowmapper |
| |
103
| + this.customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper(this.tClassInfo, this.tableToBeanField); |
| |
104
| + } |
| |
105
| + |
| |
106
| + /** |
| |
107
| + * 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br> |
| |
108
| + * 例如:HelloWorld->HELLO_WORLD |
| |
109
| + * @param name 转换前的驼峰式命名的字符串 |
| |
110
| + * @return 转换后下划线大写方式命名的字符串 |
| |
111
| + */ |
| |
112
| + public String camelToUnderline(String name) { |
| |
113
| + StringBuilder result = new StringBuilder(); |
| |
114
| + if (name != null && name.length() > 0) { |
| |
115
| + // 将第一个字符处理成大写 |
| |
116
| + result.append(name.substring(0, 1).toUpperCase()); |
| |
117
| + // 循环处理其余字符 |
| |
118
| + for (int i = 1; i < name.length(); i++) { |
| |
119
| + String s = name.substring(i, i + 1); |
| |
120
| + // 在大写字母前添加下划线 |
| |
121
| + if (Character.isUpperCase(s.charAt(0)) && Character.isLetter(s.charAt(0))) { |
| |
122
| + result.append("_"); |
| |
123
| + } |
| |
124
| + // 其他字符直接转成大写 |
| |
125
| + result.append(s.toUpperCase()); |
| |
126
| + } |
| |
127
| + } |
| |
128
| + return result.toString(); |
| |
129
| + } |
| |
130
| + |
| |
131
| + /** |
| |
132
| + * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br> |
| |
133
| + * 例如:HELLO_WORLD->HelloWorld |
| |
134
| + * @param name 转换前的下划线大写方式命名的字符串 |
| |
135
| + * @return 转换后的驼峰式命名的字符串 |
| |
136
| + */ |
| |
137
| + public String underlineToCamel(String name) { |
| |
138
| + StringBuilder result = new StringBuilder(); |
| |
139
| + // 快速检查 |
| |
140
| + if (name == null || name.isEmpty()) { |
| |
141
| + // 没必要转换 |
| |
142
| + return ""; |
| |
143
| + } else if (!name.contains("_")) { |
| |
144
| + // 不含下划线,仅将首字母小写 |
| |
145
| + return name.substring(0, 1).toLowerCase() + name.substring(1); |
| |
146
| + } |
| |
147
| + // 用下划线将原始字符串分割 |
| |
148
| + String camels[] = name.split("_"); |
| |
149
| + for (String camel : camels) { |
| |
150
| + // 跳过原始字符串中开头、结尾的下换线或双重下划线 |
| |
151
| + if (camel.isEmpty()) { |
| |
152
| + continue; |
| |
153
| + } |
| |
154
| + // 处理真正的驼峰片段 |
| |
155
| + if (result.length() == 0) { |
| |
156
| + // 第一个驼峰片段,全部字母都小写 |
| |
157
| + result.append(camel.toLowerCase()); |
| |
158
| + } else { |
| |
159
| + // 其他的驼峰片段,首字母大写 |
| |
160
| + result.append(camel.substring(0, 1).toUpperCase()); |
| |
161
| + result.append(camel.substring(1).toLowerCase()); |
| |
162
| + } |
| |
163
| + } |
| |
164
| + return result.toString(); |
| |
165
| + } |
| |
166
| + |
| |
167
| + public void appendWhereCondition(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> condition) { |
| |
168
| + if (condition == null || condition.size() == 0) return; |
| |
169
| + Object[] con = condition.get(0); |
| |
170
| + int iLen = condition.size(); |
| |
171
| + sql.append(" WHERE "); |
| |
172
| + pql.append(" WHERE "); |
| |
173
| + sql.append(con[0]); |
| |
174
| + pql.append(con[0]); |
| |
175
| + if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { |
| |
176
| + sql.append(" " + con[1] + " ?"); |
| |
177
| + pql.append(" " + con[1] + " " + con[2]); |
| |
178
| + list.add(con[2]); |
| |
179
| + } |
| |
180
| + for (int i = 1; i < iLen; i++) { |
| |
181
| + con = condition.get(i); |
| |
182
| + sql.append(" AND "); |
| |
183
| + pql.append(" AND "); |
| |
184
| + sql.append(con[0]); |
| |
185
| + pql.append(con[0]); |
| |
186
| + if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; |
| |
187
| + sql.append(" " + con[1] + " ?"); |
| |
188
| + pql.append(" " + con[1] + " " + con[2]); |
| |
189
| + list.add(con[2]); |
| |
190
| + } |
| |
191
| + } |
| |
192
| + |
| |
193
| + public void appendWhereConditionForCount(StringBuffer sql, List<Object[]> condition) { |
| |
194
| + if (condition == null || condition.size() == 0) return; |
| |
195
| + Object[] con = condition.get(0); |
| |
196
| + int iLen = condition.size(); |
| |
197
| + sql.append(" WHERE "); |
| |
198
| + sql.append(con[0]); |
| |
199
| + if (null != con[1] && con[1] != "" && con[1] != "useArg[0]") { |
| |
200
| + sql.append(" " + con[1] + " ?"); |
| |
201
| + } |
| |
202
| + for (int i = 1; i < iLen; i++) { |
| |
203
| + con = condition.get(i); |
| |
204
| + sql.append(" AND "); |
| |
205
| + sql.append(con[0]); |
| |
206
| + if (null == con[1] || "" == con[1] || con[1] == "useArg[0]") continue; |
| |
207
| + sql.append(" " + con[1] + " ?"); |
| |
208
| + } |
| |
209
| + } |
| |
210
| + |
| |
211
| + public void appendSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { |
| |
212
| + for (Object[] arg : obj) { |
| |
213
| + String opt = "="; |
| |
214
| + if (arg.length > 2) { |
| |
215
| + opt = (String)arg[2]; |
| |
216
| + } |
| |
217
| + if (opt.equals("=")) { |
| |
218
| + sql.append(" " + arg[0] + " = ?,"); |
| |
219
| + pql.append(" " + arg[0] + " = " + arg[1] + ","); |
| |
220
| + } else { |
| |
221
| + sql.append(" " + arg[0] + " = " + arg[0] + " + ?,"); |
| |
222
| + pql.append(" " + arg[0] + " = " + arg[0] + " + " + arg[1] + ","); |
| |
223
| + } |
| |
224
| + list.add(arg[1]); |
| |
225
| + } |
| |
226
| + } |
| |
227
| + |
| |
228
| + private String getTableSql(){ |
| |
229
| + return "".equals(this.dbName)?"`"+this.tableName+"`":("`"+this.dbName+"`.`"+this.tableName+"`"); |
| |
230
| + } |
| |
231
| + |
| |
232
| + /** |
| |
233
| + * 按主键查询 |
| |
234
| + */ |
| |
235
| + public T findEntityByID(ID id) { |
| |
236
| + return findEntityByID(id, true, false); |
| |
237
| + } |
| |
238
| + |
| |
239
| + /** |
| |
240
| + * 按主键查询 |
| |
241
| + * isLock 是否锁定, 默认不锁 |
| |
242
| + * fromWriteDB 是否从写库读写,默认从读库查询 |
| |
243
| + */ |
| |
244
| + public T findEntityByID(ID id, boolean fromWriteDB, boolean isLock) { |
| |
245
| + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); |
| |
246
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
247
| + sql.append(" WHERE "+idTableFieldName+" = ?"); |
| |
248
| + pql.append(" WHERE "+idTableFieldName+" = " + id); |
| |
249
| + if (isLock) { |
| |
250
| + sql.append(" FOR UPDATE"); |
| |
251
| + pql.append(" FOR UPDATE"); |
| |
252
| + } |
| |
253
| + T result = null; |
| |
254
| + try { |
| |
255
| + result = (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); |
| |
256
| + }catch(Exception e) { |
| |
257
| + UtilsLog.errorForException(e, this.getClass()); |
| |
258
| + } |
| |
259
| + return result; |
| |
260
| + } |
| |
261
| + |
| |
262
| + /** |
| |
263
| + * 根据条件List<Object[]>查询 |
| |
264
| + * Object[]数组长度是3 |
| |
265
| + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
266
| + */ |
| |
267
| + public T findEntityByCondition(List<Object[]> condition) throws Exception { |
| |
268
| + List<T> tempList = findListByCondition(condition, null, false); |
| |
269
| + if(tempList == null || tempList.size() == 0){ |
| |
270
| + return null; |
| |
271
| + } |
| |
272
| + if(tempList != null && tempList.size() == 1){ |
| |
273
| + return tempList.get(0); |
| |
274
| + }else{ |
| |
275
| + throw new Exception("数据库存在多条记录满足条件"); |
| |
276
| + } |
| |
277
| + } |
| |
278
| + |
| |
279
| + /** |
| |
280
| + * 根据条件sql查询 |
| |
281
| + * sqlCondition 为where 后面的条件。 |
| |
282
| + */ |
| |
283
| + public T findEntityBySql(String sqlCondition) throws Exception{ |
| |
284
| + List<T> tempList = findListBySql(sqlCondition, false); |
| |
285
| + if(tempList == null || tempList.size() == 0){ |
| |
286
| + return null; |
| |
287
| + } |
| |
288
| + if(tempList != null && tempList.size() == 1){ |
| |
289
| + return tempList.get(0); |
| |
290
| + }else{ |
| |
291
| + throw new Exception("数据库存在多条记录满足条件"); |
| |
292
| + } |
| |
293
| + } |
| |
294
| + |
| |
295
| + /** |
| |
296
| + * 根据条件List<Object[]>查询 |
| |
297
| + * Object[]数组长度是3 |
| |
298
| + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
299
| + */ |
| |
300
| + public List<T> findListByCondition(List<Object[]> condition) { |
| |
301
| + return findListByCondition(condition,null, false); |
| |
302
| + } |
| |
303
| + |
| |
304
| + /** |
| |
305
| + * 根据条件List<Object[]>查询 |
| |
306
| + * Object[]数组长度是3 |
| |
307
| + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
308
| + */ |
| |
309
| + public List<T> findListByCondition(List<Object[]> condition, String sortCondition, boolean fromWriteDB) { |
| |
310
| + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); |
| |
311
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
312
| + List<Object> list = new ArrayList<Object>(); |
| |
313
| + this.appendWhereCondition(sql, pql, list, condition); |
| |
314
| + if(!StringUtils.isEmpty(sortCondition)){ |
| |
315
| + sql.append(" " + sortCondition + " "); |
| |
316
| + pql.append(" " + sortCondition + " "); |
| |
317
| + } |
| |
318
| + List<T> resultList = null; |
| |
319
| + try { |
| |
320
| + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); |
| |
321
| + return resultList; |
| |
322
| + } catch (Exception e) { |
| |
323
| + UtilsLog.errorForException(e, this.getClass()); |
| |
324
| + } |
| |
325
| + return null; |
| |
326
| + } |
| |
327
| + |
| |
328
| + /** |
| |
329
| + * 根据条件sql查询 |
| |
330
| + * sqlCondition 为where 后面的条件。 |
| |
331
| + */ |
| |
332
| + public List<T> findListBySql(String sqlCondition) { |
| |
333
| + return findListBySql(sqlCondition, false); |
| |
334
| + } |
| |
335
| + |
| |
336
| + /** |
| |
337
| + * 根据条件sql查询 |
| |
338
| + * sqlCondition 为where 后面的条件。 |
| |
339
| + */ |
| |
340
| + public List<T> findListBySql(String sqlCondition, boolean fromWriteDB) { |
| |
341
| + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); |
| |
342
| + List<T> resultList = null; |
| |
343
| + try { |
| |
344
| + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); |
| |
345
| + return resultList; |
| |
346
| + } catch (Exception e) { |
| |
347
| + UtilsLog.errorForException(e, this.getClass()); |
| |
348
| + } |
| |
349
| + return null; |
| |
350
| + } |
| |
351
| + |
| |
352
| + /** |
| |
353
| + * 按条件分页查询 |
| |
354
| + * Object[]数组长度是3 |
| |
355
| + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
356
| + */ |
| |
357
| + public Map<String, Object> findPageByCondition(List<Object[]> condition,int page, int pageSize) { |
| |
358
| + return findPageByCondition(condition, null , page, pageSize, false); |
| |
359
| + } |
| |
360
| + |
| |
361
| + /** |
| |
362
| + * 按条件分页查询 |
| |
363
| + * Object[]数组长度是3 |
| |
364
| + * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
365
| + * boolean isUseCache, 是否用缓存,默认用。 |
| |
366
| + * boolean isAddCache, 是否添加缓存,默认添加。 |
| |
367
| + */ |
| |
368
| + public Map<String, Object> findPageByCondition(List<Object[]> condition,String sortCondition, int page, int pageSize, boolean fromWriteDB) { |
| |
369
| + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()); |
| |
370
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
371
| + StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) rowCount FROM "+this.getTableSql()); |
| |
372
| + |
| |
373
| + List<Object> count_list = new ArrayList<Object>(); |
| |
374
| + List<Object> page_list = new ArrayList<Object>(); |
| |
375
| + this.appendWhereConditionForCount(sqlCount, condition); |
| |
376
| + this.appendWhereCondition(sql, pql, count_list, condition); |
| |
377
| + for (int i = 0; i < count_list.size(); i++) |
| |
378
| + page_list.add(count_list.get(i)); |
| |
379
| + |
| |
380
| + page_list.add((page - 1) * pageSize); |
| |
381
| + page_list.add(pageSize); |
| |
382
| + |
| |
383
| + if(!StringUtils.isEmpty(sortCondition)){ |
| |
384
| + sql.append(" " + sortCondition + " "); |
| |
385
| + pql.append(" " + sortCondition + " "); |
| |
386
| + } |
| |
387
| + |
| |
388
| + String pageSql = sql.toString() + " limit ?, ?"; |
| |
389
| + |
| |
390
| + List<T> resultList = null; |
| |
391
| + try { |
| |
392
| + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; |
| |
393
| + |
| |
394
| + Map<String, Object> resultMap = new HashMap<String, Object>(); |
| |
395
| + resultMap.put("page", page); |
| |
396
| + resultMap.put("total", totalRowsMap.get("rowCount")); |
| |
397
| + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); |
| |
398
| + resultMap.put("rows", resultList); |
| |
399
| + return resultMap; |
| |
400
| + } catch (Exception e) { |
| |
401
| + UtilsLog.errorForException(e, this.getClass()); |
| |
402
| + } |
| |
403
| + return null; |
| |
404
| + } |
| |
405
| + |
| |
406
| + /** |
| |
407
| + * 按sql分页查询, sqlCondition为where 后条件sql |
| |
408
| + */ |
| |
409
| + public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize) { |
| |
410
| + return findPageBySql(sqlCondition, page, pageSize, false); |
| |
411
| + } |
| |
412
| + |
| |
413
| + /** |
| |
414
| + * 按sql分页查询, sqlCondition为where 后条件sql |
| |
415
| + */ |
| |
416
| + public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB) { |
| |
417
| + StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition ); |
| |
418
| + StringBuffer sqlCount = new StringBuffer("SELECT count(1) rowCount FROM "+this.getTableSql()+" WHERE " + sqlCondition); |
| |
419
| + String pageSql = sql.toString() + " limit ?, ?"; |
| |
420
| + String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); |
| |
421
| + List<Object> page_list = new ArrayList<Object>(); |
| |
422
| + page_list.add((page - 1) * pageSize); |
| |
423
| + page_list.add(page * pageSize); |
| |
424
| + |
| |
425
| + |
| |
426
| + List<T> resultList = null; |
| |
427
| + try { |
| |
428
| + |
| |
429
| + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); |
| |
430
| + |
| |
431
| + Map<String, Object> resultMap = new HashMap<String, Object>(); |
| |
432
| + resultMap.put("page", page); |
| |
433
| + resultMap.put("total", totalRowsMap.get("rowCount")); |
| |
434
| + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); |
| |
435
| + resultMap.put("rows", resultList); |
| |
436
| + return resultMap; |
| |
437
| + } catch (Exception e) { |
| |
438
| + UtilsLog.errorForException(e, this.getClass()); |
| |
439
| + } |
| |
440
| + return null; |
| |
441
| + } |
| |
442
| + |
| |
443
| + /** |
| |
444
| + * 添加 |
| |
445
| + */ |
| |
446
| + public BigInteger addEntityForAutoincrementId(T entity) { |
| |
447
| + StringBuffer sqlForLog = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); |
| |
448
| + StringBuffer sqlValueForLog = new StringBuffer(") VALUES ("); |
| |
449
| + StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); |
| |
450
| + StringBuffer sqlColumnPart = new StringBuffer(") VALUES ("); |
| |
451
| + List<Object> paramList = new ArrayList<Object>(); |
| |
452
| + |
| |
453
| + Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); |
| |
454
| + while(beanFieldIter.hasNext()){ |
| |
455
| + String beanFieldName = beanFieldIter.next(); |
| |
456
| + String tableFieldName = this.beanToTableField.get(beanFieldName); |
| |
457
| + Field beanField; |
| |
458
| + Object beanFieldValue = null; |
| |
459
| + try { |
| |
460
| + beanField = this.tClassInfo.getDeclaredField(beanFieldName); |
| |
461
| + beanField.setAccessible(true); |
| |
462
| + beanFieldValue = beanField.get(entity); |
| |
463
| + } catch (Exception e) { |
| |
464
| + e.printStackTrace(); |
| |
465
| + } |
| |
466
| + |
| |
467
| + if(tableFieldName == null || beanFieldName == null || beanFieldValue == null){ |
| |
468
| + continue; |
| |
469
| + } |
| |
470
| + |
| |
471
| + sqlForLog.append("`"+tableFieldName+"`,"); |
| |
472
| + sqlValueForLog.append(beanFieldValue.toString()+","); |
| |
473
| + sqlInsertPart.append("`"+tableFieldName+"`,"); |
| |
474
| + sqlColumnPart.append(" ?,"); |
| |
475
| + paramList.add(beanFieldValue); |
| |
476
| + } |
| |
477
| + //打印日志内容 |
| |
478
| + sqlForLog.substring(0, sqlForLog.length()-1); |
| |
479
| + sqlForLog.append(") VALUES ("); |
| |
480
| + sqlForLog.append(sqlValueForLog+")"); |
| |
481
| + //UtilsLog.infoForMessage(sqlForLog.toString(), this.getClass()); |
| |
482
| + |
| |
483
| + //执行SQL |
| |
484
| + String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
| |
485
| + try { |
| |
486
| + jdbcTemplateWrite.update(exeSql, paramList.toArray()); |
| |
487
| + Map<String, Object> lastInsertIdMap = jdbcTemplateWrite.queryForMap("SELECT LAST_INSERT_ID() lastInsertId"); |
| |
488
| + return (BigInteger)lastInsertIdMap.get("lastInsertId"); |
| |
489
| + } catch (Exception e) { |
| |
490
| + UtilsLog.errorForException(e, this.getClass()); |
| |
491
| + } |
| |
492
| + return null; |
| |
493
| + } |
| |
494
| + |
| |
495
| + /** |
| |
496
| + * 批量添加 |
| |
497
| + */ |
| |
498
| + public List<BigInteger> addEntityList(List<T> entityList) { |
| |
499
| + List<BigInteger> result = new ArrayList<BigInteger>(); |
| |
500
| + for (T entity : entityList) { |
| |
501
| + result.add(addEntityForAutoincrementId(entity)); |
| |
502
| + } |
| |
503
| + return result; |
| |
504
| + } |
| |
505
| + |
| |
506
| + /** |
| |
507
| + * 按ID删除 |
| |
508
| + */ |
| |
509
| + public int deleteEntityByID(ID id) { |
| |
510
| + StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE"); |
| |
511
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
512
| + pql.append(" "+this.idTableFieldName+" = " + id); |
| |
513
| + sql.append(" "+this.idTableFieldName+" = ?"); |
| |
514
| + //UtilsLog.infoForMessage(pql.toString(), this.getClass()); |
| |
515
| + return jdbcTemplateWrite.update(sql.toString(), id); |
| |
516
| + } |
| |
517
| + /** |
| |
518
| + * 删除按List<Object[]>条件 |
| |
519
| + * Object[]数组长度是3 |
| |
520
| + * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
521
| + */ |
| |
522
| + public int deleteEntityByCondition(List<Object[]> condition) throws Exception{ |
| |
523
| + if (null == condition || condition.size() == 0) { |
| |
524
| + throw new Exception("没有传入条件,请至少传入一个筛选条件"); |
| |
525
| + } |
| |
526
| + |
| |
527
| + List<Object> list = new ArrayList<Object>(); |
| |
528
| + StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); |
| |
529
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
530
| + this.appendWhereCondition(sql, pql, list, condition); |
| |
531
| + try { |
| |
532
| + return jdbcTemplateWrite.update( sql.toString(), list.toArray()); |
| |
533
| + } catch (Exception e) { |
| |
534
| + UtilsLog.errorForException(e, this.getClass()); |
| |
535
| + } |
| |
536
| + return 0; |
| |
537
| + } |
| |
538
| + |
| |
539
| + /** |
| |
540
| + * 删除按condition条件 |
| |
541
| + * 建议使用deleteTByCondition(List<Object[]> condition), 如果removeTByCondition(List<Object[]> condition)满足不了where条件可以使用此方法。 |
| |
542
| + * condition为where后面的条件,condition不能为空。 |
| |
543
| + */ |
| |
544
| + public int deleteEntityBySql(String sqlCondition) throws Exception{ |
| |
545
| + if("".equals(sqlCondition)) { |
| |
546
| + throw new Exception("没有传入条件,请至少传入一个筛选条件"); |
| |
547
| + } |
| |
548
| + StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE "); |
| |
549
| + try { |
| |
550
| + return jdbcTemplateWrite.update( sql.toString() + sqlCondition); |
| |
551
| + } catch (Exception e) { |
| |
552
| + UtilsLog.errorForException(e, this.getClass()); |
| |
553
| + } |
| |
554
| + return 0; |
| |
555
| + } |
| |
556
| + |
| |
557
| + /** |
| |
558
| + * 根据list对象逐个删除。 |
| |
559
| + */ |
| |
560
| + public List<Integer> deleteEntityList(List<T> entityList) { |
| |
561
| + List<Integer> result = new ArrayList<Integer>(); |
| |
562
| + for (T entity : entityList) { |
| |
563
| + Field beanField; |
| |
564
| + Object beanFieldValue = Integer.valueOf(0); |
| |
565
| + try { |
| |
566
| + beanField = this.tClassInfo.getDeclaredField(this.idBeanFieldName); |
| |
567
| + beanField.setAccessible(true); |
| |
568
| + beanFieldValue = beanField.get(entity); |
| |
569
| + } catch (Exception e) { |
| |
570
| + UtilsLog.errorForException(e, this.getClass()); |
| |
571
| + } |
| |
572
| + result.add(deleteEntityByID((ID)beanFieldValue)); |
| |
573
| + } |
| |
574
| + return result; |
| |
575
| + } |
| |
576
| + |
| |
577
| + /** |
| |
578
| + * 根据ID修改指定的值 |
| |
579
| + */ |
| |
580
| + public int updateEntityById(List<Object[]> changeList, ID id) throws Exception{ |
| |
581
| + if(null == id){ |
| |
582
| + throw new Exception("请求条件异常,没有传入主键ID!"); |
| |
583
| + } |
| |
584
| + |
| |
585
| + StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
| |
586
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
587
| + List<Object> list = new ArrayList<Object>(); |
| |
588
| + for (int i = 0, iLen = changeList.size(); i < iLen; i++) { |
| |
589
| + String name = (String) changeList.get(i)[0]; |
| |
590
| + Object value = changeList.get(i)[1]; |
| |
591
| + String variationOpt = "="; |
| |
592
| + if (changeList.get(i).length > 2) |
| |
593
| + variationOpt = (String)changeList.get(i)[2]; |
| |
594
| + if (variationOpt.equals("=")) { |
| |
595
| + sql.append(" " + name + " = ?,"); |
| |
596
| + list.add(value); |
| |
597
| + if (value == null) { |
| |
598
| + pql.append(" " + name + "=null,"); |
| |
599
| + } else { |
| |
600
| + pql.append(" " + name + "=\"" + value.toString() + "\","); |
| |
601
| + } |
| |
602
| + } else { |
| |
603
| + sql.append(" " + name + " = " + name + " + ?,"); |
| |
604
| + list.add(value); |
| |
605
| + pql.append(" " + name + " = " + name + " + " + value.toString() + ","); |
| |
606
| + } |
| |
607
| + } |
| |
608
| + |
| |
609
| + //记录SQL |
| |
610
| + String pwhere = " WHERE "+this.idTableFieldName+"=\"" + id + "\""; |
| |
611
| + //UtilsLog.infoForMessage(pql.toString()+pwhere, this.getClass()); |
| |
612
| + |
| |
613
| + try { |
| |
614
| + String where = " WHERE "+this.idTableFieldName+"=?"; |
| |
615
| + String updateSql = sql.substring(0, sql.length()-1)+where; |
| |
616
| + list.add(id); |
| |
617
| + return jdbcTemplateWrite.update(updateSql, list.toArray()); |
| |
618
| + } catch (Exception e) { |
| |
619
| + UtilsLog.errorForException(e, this.getClass()); |
| |
620
| + } |
| |
621
| + return 0; |
| |
622
| + } |
| |
623
| + |
| |
624
| + /** |
| |
625
| + * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 |
| |
626
| + * List<Object[]> condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 |
| |
627
| + */ |
| |
628
| + public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition) throws Exception{ |
| |
629
| + if (null == updateObj || updateObj.size() == 0) { |
| |
630
| + throw new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"); |
| |
631
| + } |
| |
632
| + if (null == condition || condition.size() == 0) { |
| |
633
| + throw new Exception("请求条件异常,请求条件List<Object[]> condition不能为空!"); |
| |
634
| + } |
| |
635
| + |
| |
636
| + StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
| |
637
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
638
| + List<Object> list = new ArrayList<Object>(); |
| |
639
| + this.appendSql(sql, pql, list, updateObj); |
| |
640
| + |
| |
641
| + StringBuffer where = new StringBuffer(""); |
| |
642
| + StringBuffer pwhere = new StringBuffer(""); |
| |
643
| + this.appendWhereCondition(where, pwhere, list, condition); |
| |
644
| + |
| |
645
| + //UtilsLog.infoForMessage(pql.toString()+pwhere.toString(), this.getClass()); |
| |
646
| + |
| |
647
| + String updateSql = sql.substring(0, sql.length()-1)+where.toString(); |
| |
648
| + try { |
| |
649
| + return jdbcTemplateWrite.update(updateSql, list.toArray()); |
| |
650
| + } catch (Exception e) { |
| |
651
| + UtilsLog.errorForException(e, this.getClass()); |
| |
652
| + } |
| |
653
| + return 0; |
| |
654
| + } |
| |
655
| + |
| |
656
| + /** |
| |
657
| + * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 |
| |
658
| + * String sqlCondition 修改的条件。 |
| |
659
| + */ |
| |
660
| + public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition) throws Exception{ |
| |
661
| + if (null == updateObj || updateObj.size() == 0) { |
| |
662
| + throw new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"); |
| |
663
| + } |
| |
664
| + if ("".equals(sqlCondition)) { |
| |
665
| + throw new Exception("请求条件异常,请求条件sqlCondition不能为空!"); |
| |
666
| + } |
| |
667
| + |
| |
668
| + StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
| |
669
| + StringBuffer pql = new StringBuffer(sql.toString()); |
| |
670
| + List<Object> list = new ArrayList<Object>(); |
| |
671
| + this.appendSql(sql, pql, list, updateObj); |
| |
672
| + |
| |
673
| + try { |
| |
674
| + String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; |
| |
675
| + return jdbcTemplateWrite.update(updateSql, list.toArray()); |
| |
676
| + } catch (Exception e) { |
| |
677
| + UtilsLog.errorForException(e, this.getClass()); |
| |
678
| + } |
| |
679
| + return 0; |
| |
680
| + } |
| |
681
| + |
| |
682
| + public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ |
| |
683
| + //构造查询语句 |
| |
684
| + String querySql = coreSql+orderByPartSql+UtilsSql.getLimitCondition(page, pageSize); |
| |
685
| + |
| |
686
| + //构造统计计数语句 |
| |
687
| + String countSql = "select count(*) rows from ("+coreSql+" ) t "; |
| |
688
| + |
| |
689
| + //执行查询 |
| |
690
| + List<Map<String, Object>> queryData = new ArrayList<Map<String, Object>>(); |
| |
691
| + Map<String, Object> countData = new HashMap<String, Object>(); |
| |
692
| + try{ |
| |
693
| + queryData = this.jdbcTemplateRead.queryForList(querySql); |
| |
694
| + countData = this.jdbcTemplateRead.queryForMap(countSql); |
| |
695
| + }catch(Exception e){ |
| |
696
| + countData.put("rows", "0"); |
| |
697
| + |
| |
698
| + UtilsLog.errorForException(e, this.getClass()); |
| |
699
| + } |
| |
700
| + |
| |
701
| + return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); |
| |
702
| + } |
| |
703
| +} |