|
|
|
1
|
+package com.zhonglai.luhui.dao.dto;
|
|
|
|
2
|
+
|
|
|
|
3
|
+import com.fasterxml.jackson.annotation.JsonFormat;
|
|
|
|
4
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
|
5
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
|
6
|
+import com.zhonglai.luhui.dao.sql.MyBaseEntity;
|
|
|
|
7
|
+import com.zhonglai.luhui.dao.sql.QueryType;
|
|
|
|
8
|
+import com.zhonglai.luhui.dao.util.Sqlutil;
|
|
|
|
9
|
+
|
|
|
|
10
|
+import java.lang.reflect.Field;
|
|
|
|
11
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
12
|
+import java.lang.reflect.Method;
|
|
|
|
13
|
+import java.text.MessageFormat;
|
|
|
|
14
|
+import java.util.Date;
|
|
|
|
15
|
+import java.util.List;
|
|
|
|
16
|
+import java.util.Map;
|
|
|
|
17
|
+
|
|
|
|
18
|
+public class PublicTemplateSQL {
|
|
|
|
19
|
+ private String changTableNameFromObject(Object object)
|
|
|
|
20
|
+ {
|
|
|
|
21
|
+ return Sqlutil.toUnderScoreCase(object.getClass().getSimpleName());
|
|
|
|
22
|
+ }
|
|
|
|
23
|
+
|
|
|
|
24
|
+
|
|
|
|
25
|
+ private String insertListSql(List<?> list, String tableName)
|
|
|
|
26
|
+ {
|
|
|
|
27
|
+ if(StringUtils.isBlank(tableName))
|
|
|
|
28
|
+ {
|
|
|
|
29
|
+ tableName = changTableNameFromObject(list.get(0));
|
|
|
|
30
|
+ }
|
|
|
|
31
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
32
|
+ sb.append("INSERT INTO "+tableName);
|
|
|
|
33
|
+ String values = "";
|
|
|
|
34
|
+ String mfStr = "";
|
|
|
|
35
|
+
|
|
|
|
36
|
+ Field[] fields = list.get(0).getClass().getDeclaredFields( );
|
|
|
|
37
|
+ Method[] methods = list.get(0).getClass().getMethods();
|
|
|
|
38
|
+ for(Field field:fields)
|
|
|
|
39
|
+ {
|
|
|
|
40
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
41
|
+ {
|
|
|
|
42
|
+ continue;
|
|
|
|
43
|
+ }
|
|
|
|
44
|
+ if(!"".equals(values) )
|
|
|
|
45
|
+ {
|
|
|
|
46
|
+ mfStr += ",";
|
|
|
|
47
|
+ values += ",";
|
|
|
|
48
|
+ }
|
|
|
|
49
|
+ values += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`";
|
|
|
|
50
|
+ mfStr += "#'{'list[{0}]."+field.getName()+"}";
|
|
|
|
51
|
+ }
|
|
|
|
52
|
+ sb.append("("+values+")");
|
|
|
|
53
|
+ sb.append("VALUES ");
|
|
|
|
54
|
+ MessageFormat mf = new MessageFormat("("+mfStr+")");
|
|
|
|
55
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
|
56
|
+ sb.append(mf.format(new Object[]{i}));
|
|
|
|
57
|
+ if (i < list.size() - 1) {
|
|
|
|
58
|
+ sb.append(",");
|
|
|
|
59
|
+ }
|
|
|
|
60
|
+ }
|
|
|
|
61
|
+ return sb.toString();
|
|
|
|
62
|
+ }
|
|
|
|
63
|
+
|
|
|
|
64
|
+ public String insertAllToTable(Map map)
|
|
|
|
65
|
+ {
|
|
|
|
66
|
+ List<?> list = (List<?>) map.get("list");
|
|
|
|
67
|
+ String tableName = (String) map.get("tableName");
|
|
|
|
68
|
+ return insertListSql(list,tableName);
|
|
|
|
69
|
+ }
|
|
|
|
70
|
+ /**
|
|
|
|
71
|
+ * 添加对象
|
|
|
|
72
|
+ * @return
|
|
|
|
73
|
+ */
|
|
|
|
74
|
+ public String insertAll(Map map)
|
|
|
|
75
|
+ {
|
|
|
|
76
|
+ List<?> list = (List<?>) map.get("list");
|
|
|
|
77
|
+ return insertListSql(list,null);
|
|
|
|
78
|
+ }
|
|
|
|
79
|
+
|
|
|
|
80
|
+ /**
|
|
|
|
81
|
+ * 添加对象
|
|
|
|
82
|
+ * @param object 对象
|
|
|
|
83
|
+ * @return
|
|
|
|
84
|
+ */
|
|
|
|
85
|
+ public String insert(Object object)
|
|
|
|
86
|
+ {
|
|
|
|
87
|
+ StringBuffer stringBuffer = new StringBuffer("insert into ");
|
|
|
|
88
|
+
|
|
|
|
89
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
90
|
+ stringBuffer.append(tableName + "(");
|
|
|
|
91
|
+ Field[] fields = object.getClass().getDeclaredFields( );
|
|
|
|
92
|
+ Method[] methods = object.getClass().getMethods( );
|
|
|
|
93
|
+
|
|
|
|
94
|
+ StringBuffer values = new StringBuffer("(");
|
|
|
|
95
|
+ for(Field field:fields)
|
|
|
|
96
|
+ {//
|
|
|
|
97
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
98
|
+ {
|
|
|
|
99
|
+ continue;
|
|
|
|
100
|
+ }
|
|
|
|
101
|
+ Method method;
|
|
|
|
102
|
+ try {
|
|
|
|
103
|
+ method = object.getClass().getMethod("get"+Sqlutil.getName(field.getName()));
|
|
|
|
104
|
+ Object value = method.invoke(object);
|
|
|
|
105
|
+ if(null != value)
|
|
|
|
106
|
+ {
|
|
|
|
107
|
+ if(!"(".equals(values.toString()) )
|
|
|
|
108
|
+ {
|
|
|
|
109
|
+ stringBuffer.append(",");
|
|
|
|
110
|
+ values.append( ",");
|
|
|
|
111
|
+ }
|
|
|
|
112
|
+ stringBuffer.append( "`"+Sqlutil.toUnderScoreCase(field.getName())+"`");
|
|
|
|
113
|
+ if (field.isAnnotationPresent(JsonFormat.class) && value instanceof Date)
|
|
|
|
114
|
+ {
|
|
|
|
115
|
+ JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class);
|
|
|
|
116
|
+ value = DateUtils.parseDateToStr(jsonFormat.pattern(), (Date) value);
|
|
|
|
117
|
+ }
|
|
|
|
118
|
+ values.append( "'"+ escapeSql(value+"")+"'");
|
|
|
|
119
|
+ }
|
|
|
|
120
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
121
|
+ // TODO Auto-generated catch block
|
|
|
|
122
|
+ e.printStackTrace();
|
|
|
|
123
|
+ } catch (SecurityException e) {
|
|
|
|
124
|
+ // TODO Auto-generated catch block
|
|
|
|
125
|
+ e.printStackTrace();
|
|
|
|
126
|
+ } catch (IllegalAccessException e) {
|
|
|
|
127
|
+ // TODO Auto-generated catch block
|
|
|
|
128
|
+ e.printStackTrace();
|
|
|
|
129
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
130
|
+ // TODO Auto-generated catch block
|
|
|
|
131
|
+ e.printStackTrace();
|
|
|
|
132
|
+ } catch (InvocationTargetException e) {
|
|
|
|
133
|
+ // TODO Auto-generated catch block
|
|
|
|
134
|
+ e.printStackTrace();
|
|
|
|
135
|
+ }
|
|
|
|
136
|
+
|
|
|
|
137
|
+
|
|
|
|
138
|
+ }
|
|
|
|
139
|
+ stringBuffer.append( ")");
|
|
|
|
140
|
+ values.append( ")");
|
|
|
|
141
|
+ return stringBuffer.append(" values ").append(values).toString();
|
|
|
|
142
|
+ }
|
|
|
|
143
|
+
|
|
|
|
144
|
+ /**
|
|
|
|
145
|
+ * 指定表名添加对象
|
|
|
|
146
|
+ * @return
|
|
|
|
147
|
+ */
|
|
|
|
148
|
+ public String insertToTable(Map map)
|
|
|
|
149
|
+ {
|
|
|
|
150
|
+ Object object = map.get("object");
|
|
|
|
151
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
152
|
+ String primaryKey = (String) map.get("primaryKey");
|
|
|
|
153
|
+ if(map.containsKey("tableName"))
|
|
|
|
154
|
+ {
|
|
|
|
155
|
+ tableName = map.get("tableName").toString();
|
|
|
|
156
|
+ }
|
|
|
|
157
|
+ StringBuffer sql = new StringBuffer();
|
|
|
|
158
|
+ sql.append("insert into ");
|
|
|
|
159
|
+ sql.append(tableName + "(");
|
|
|
|
160
|
+ Field[] fields = object.getClass().getDeclaredFields( );
|
|
|
|
161
|
+ String values = "(";
|
|
|
|
162
|
+ Method[] methods = object.getClass().getMethods();
|
|
|
|
163
|
+ for(Field field:fields)
|
|
|
|
164
|
+ {
|
|
|
|
165
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
166
|
+ {
|
|
|
|
167
|
+ continue;
|
|
|
|
168
|
+ }
|
|
|
|
169
|
+ Method method;
|
|
|
|
170
|
+ try {
|
|
|
|
171
|
+ method = object.getClass().getMethod("get"+Sqlutil.getName(field.getName()));
|
|
|
|
172
|
+ Object value = method.invoke(object);
|
|
|
|
173
|
+ if(null != value)
|
|
|
|
174
|
+ {
|
|
|
|
175
|
+ if(!"(".equals(values) )
|
|
|
|
176
|
+ {
|
|
|
|
177
|
+ sql.append(",");
|
|
|
|
178
|
+ values += ",";
|
|
|
|
179
|
+ }
|
|
|
|
180
|
+ sql.append("`"+Sqlutil.toUnderScoreCase(field.getName())+"`");
|
|
|
|
181
|
+ if (field.isAnnotationPresent(JsonFormat.class) && value instanceof Date)
|
|
|
|
182
|
+ {
|
|
|
|
183
|
+ JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class);
|
|
|
|
184
|
+ value = DateUtils.parseDateToStr(jsonFormat.pattern(), (Date) value);
|
|
|
|
185
|
+ }
|
|
|
|
186
|
+ values += "'"+escapeSql(value+"")+"'";
|
|
|
|
187
|
+ }
|
|
|
|
188
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
189
|
+ // TODO Auto-generated catch block
|
|
|
|
190
|
+ e.printStackTrace();
|
|
|
|
191
|
+ } catch (SecurityException e) {
|
|
|
|
192
|
+ // TODO Auto-generated catch block
|
|
|
|
193
|
+ e.printStackTrace();
|
|
|
|
194
|
+ } catch (IllegalAccessException e) {
|
|
|
|
195
|
+ // TODO Auto-generated catch block
|
|
|
|
196
|
+ e.printStackTrace();
|
|
|
|
197
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
198
|
+ // TODO Auto-generated catch block
|
|
|
|
199
|
+ e.printStackTrace();
|
|
|
|
200
|
+ } catch (InvocationTargetException e) {
|
|
|
|
201
|
+ // TODO Auto-generated catch block
|
|
|
|
202
|
+ e.printStackTrace();
|
|
|
|
203
|
+ }
|
|
|
|
204
|
+
|
|
|
|
205
|
+
|
|
|
|
206
|
+ }
|
|
|
|
207
|
+ sql.append(")");
|
|
|
|
208
|
+ values += ")";
|
|
|
|
209
|
+ return sql.toString()+" values "+values;
|
|
|
|
210
|
+ }
|
|
|
|
211
|
+
|
|
|
|
212
|
+ /**
|
|
|
|
213
|
+ * 更新对象不为空的属性
|
|
|
|
214
|
+ * @param para
|
|
|
|
215
|
+ * @return
|
|
|
|
216
|
+ */
|
|
|
|
217
|
+ public String updateObject(Map<String,Object> para)
|
|
|
|
218
|
+ {
|
|
|
|
219
|
+ Object object = para.get("object");
|
|
|
|
220
|
+ String whereFieldNames = (String) para.get("whereFieldNames");
|
|
|
|
221
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
222
|
+ if(para.containsKey("tablename"))
|
|
|
|
223
|
+ {
|
|
|
|
224
|
+ tableName = (String) para.get("tablename");
|
|
|
|
225
|
+ }
|
|
|
|
226
|
+ String sql = "update ";
|
|
|
|
227
|
+ sql += tableName;
|
|
|
|
228
|
+ Field[] fields = object.getClass().getDeclaredFields();
|
|
|
|
229
|
+ Method[] methods = object.getClass().getMethods();
|
|
|
|
230
|
+ if(null != fields && fields.length !=0 )
|
|
|
|
231
|
+ {
|
|
|
|
232
|
+ sql += " set ";
|
|
|
|
233
|
+ int j = 0;
|
|
|
|
234
|
+ for(int i=0;i<fields.length;i++)
|
|
|
|
235
|
+ {
|
|
|
|
236
|
+ Field field = fields[i];
|
|
|
|
237
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
238
|
+ {
|
|
|
|
239
|
+ continue;
|
|
|
|
240
|
+ }
|
|
|
|
241
|
+ try {
|
|
|
|
242
|
+ Method method = object.getClass().getMethod("get"+Sqlutil.getName(field.getName()));
|
|
|
|
243
|
+ Object value = method.invoke(object);
|
|
|
|
244
|
+ if(null != value)
|
|
|
|
245
|
+ {
|
|
|
|
246
|
+ if(j!=0)
|
|
|
|
247
|
+ {
|
|
|
|
248
|
+ sql += ",";
|
|
|
|
249
|
+ }
|
|
|
|
250
|
+
|
|
|
|
251
|
+ sql += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+"='"+escapeSql(value+"")+"'";
|
|
|
|
252
|
+ j++;
|
|
|
|
253
|
+
|
|
|
|
254
|
+// if(i!=0)
|
|
|
|
255
|
+// {
|
|
|
|
256
|
+// sql += ",";
|
|
|
|
257
|
+// }
|
|
|
|
258
|
+// sql += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+"='"+value+"'";
|
|
|
|
259
|
+ }
|
|
|
|
260
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
261
|
+
|
|
|
|
262
|
+ e.printStackTrace();
|
|
|
|
263
|
+ } catch (SecurityException e) {
|
|
|
|
264
|
+ // TODO Auto-generated catch block
|
|
|
|
265
|
+ e.printStackTrace();
|
|
|
|
266
|
+ } catch (IllegalAccessException e) {
|
|
|
|
267
|
+ // TODO Auto-generated catch block
|
|
|
|
268
|
+ e.printStackTrace();
|
|
|
|
269
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
270
|
+ // TODO Auto-generated catch block
|
|
|
|
271
|
+ e.printStackTrace();
|
|
|
|
272
|
+ } catch (InvocationTargetException e) {
|
|
|
|
273
|
+ // TODO Auto-generated catch block
|
|
|
|
274
|
+ e.printStackTrace();
|
|
|
|
275
|
+ }
|
|
|
|
276
|
+
|
|
|
|
277
|
+ }
|
|
|
|
278
|
+
|
|
|
|
279
|
+ sql += " where 1=1 ";
|
|
|
|
280
|
+ String[] wheres = whereFieldNames.split(",");
|
|
|
|
281
|
+ if(StringUtils.isNotBlank(whereFieldNames))
|
|
|
|
282
|
+ {
|
|
|
|
283
|
+ for(int i =0;i<wheres.length;i++)
|
|
|
|
284
|
+ {
|
|
|
|
285
|
+ try {
|
|
|
|
286
|
+ Method method = object.getClass().getMethod("get"+Sqlutil.getName(wheres[i]));
|
|
|
|
287
|
+ Object value = method.invoke(object);
|
|
|
|
288
|
+ sql += " and ";
|
|
|
|
289
|
+ sql += Sqlutil.toUnderScoreCase(wheres[i]) + "='"+escapeSql(value+"")+"'";
|
|
|
|
290
|
+// sql += Sqlutil.getName(wheres[i]) + "='"+value+"'";
|
|
|
|
291
|
+ } catch (IllegalAccessException e) {
|
|
|
|
292
|
+ // TODO Auto-generated catch block
|
|
|
|
293
|
+ e.printStackTrace();
|
|
|
|
294
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
295
|
+ // TODO Auto-generated catch block
|
|
|
|
296
|
+ e.printStackTrace();
|
|
|
|
297
|
+ } catch (InvocationTargetException e) {
|
|
|
|
298
|
+ // TODO Auto-generated catch block
|
|
|
|
299
|
+ e.printStackTrace();
|
|
|
|
300
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
301
|
+ // TODO Auto-generated catch block
|
|
|
|
302
|
+ e.printStackTrace();
|
|
|
|
303
|
+ } catch (SecurityException e) {
|
|
|
|
304
|
+ // TODO Auto-generated catch block
|
|
|
|
305
|
+ e.printStackTrace();
|
|
|
|
306
|
+ }
|
|
|
|
307
|
+
|
|
|
|
308
|
+ }
|
|
|
|
309
|
+ }
|
|
|
|
310
|
+
|
|
|
|
311
|
+
|
|
|
|
312
|
+ }
|
|
|
|
313
|
+ return sql;
|
|
|
|
314
|
+ }
|
|
|
|
315
|
+
|
|
|
|
316
|
+ /**
|
|
|
|
317
|
+ * 获得对象
|
|
|
|
318
|
+ */
|
|
|
|
319
|
+ public String getObject(Map<String, Object> para)
|
|
|
|
320
|
+ {
|
|
|
|
321
|
+ Class<?> clas = (Class<?>) para.get("class");
|
|
|
|
322
|
+ String idName = (String) para.get("idName");
|
|
|
|
323
|
+ String values = (String) para.get("values");
|
|
|
|
324
|
+ String tableName = null;
|
|
|
|
325
|
+
|
|
|
|
326
|
+ if(para.containsKey("tableName"))
|
|
|
|
327
|
+ {
|
|
|
|
328
|
+ tableName = (String) para.get("tableName");
|
|
|
|
329
|
+ }
|
|
|
|
330
|
+
|
|
|
|
331
|
+ String select = "";
|
|
|
|
332
|
+ if(para.containsKey("select") && null != para.get("select"))
|
|
|
|
333
|
+ {
|
|
|
|
334
|
+ select = (String) para.get("select");
|
|
|
|
335
|
+ }else{
|
|
|
|
336
|
+ select = getSelect(clas);
|
|
|
|
337
|
+ }
|
|
|
|
338
|
+
|
|
|
|
339
|
+ if(StringUtils.isBlank(tableName))
|
|
|
|
340
|
+ {
|
|
|
|
341
|
+ tableName = Sqlutil.toUnderScoreCase(clas.getSimpleName());
|
|
|
|
342
|
+ }
|
|
|
|
343
|
+
|
|
|
|
344
|
+ String[] idnames = idName.split(",");
|
|
|
|
345
|
+ String[] valuess = values.split(",");
|
|
|
|
346
|
+
|
|
|
|
347
|
+ String where = "";
|
|
|
|
348
|
+ for(int i=0;i<idnames.length;i++)
|
|
|
|
349
|
+ {
|
|
|
|
350
|
+ if(i != 0)
|
|
|
|
351
|
+ {
|
|
|
|
352
|
+ where += " and ";
|
|
|
|
353
|
+ }
|
|
|
|
354
|
+ where += "`"+Sqlutil.toUnderScoreCase(idnames[i])+"`='"+escapeSql(valuess[i]+"")+"'";
|
|
|
|
355
|
+
|
|
|
|
356
|
+ }
|
|
|
|
357
|
+ String sql = "select "+select+" from "+tableName + " where " +where;
|
|
|
|
358
|
+ return sql;
|
|
|
|
359
|
+ }
|
|
|
|
360
|
+
|
|
|
|
361
|
+ /**
|
|
|
|
362
|
+ * 通过条件删除数据
|
|
|
|
363
|
+ * @param para
|
|
|
|
364
|
+ * @return
|
|
|
|
365
|
+ */
|
|
|
|
366
|
+ public String deleteObjectByContent(Map<String, Object> para)
|
|
|
|
367
|
+ {
|
|
|
|
368
|
+ Class<?> objectCalss = (Class<?>) para.get("objectCalss");
|
|
|
|
369
|
+ String tableName = null;
|
|
|
|
370
|
+ if(para.containsKey("tableName"))
|
|
|
|
371
|
+ {
|
|
|
|
372
|
+ tableName = (String) para.get("tableName");
|
|
|
|
373
|
+ }
|
|
|
|
374
|
+
|
|
|
|
375
|
+ @SuppressWarnings("unchecked")
|
|
|
|
376
|
+ Map<String,String> map = (Map<String, String>) para.get("map");
|
|
|
|
377
|
+
|
|
|
|
378
|
+ if(StringUtils.isBlank(tableName))
|
|
|
|
379
|
+ {
|
|
|
|
380
|
+ tableName = Sqlutil.toUnderScoreCase(objectCalss.getSimpleName());
|
|
|
|
381
|
+ }
|
|
|
|
382
|
+ String sql = "delete from ";
|
|
|
|
383
|
+ sql += tableName + " where 1=1 ";
|
|
|
|
384
|
+ for(String key:map.keySet())
|
|
|
|
385
|
+ {
|
|
|
|
386
|
+ sql += " and "+"`"+Sqlutil.toUnderScoreCase(key)+"`"+"='"+escapeSql(map.get(key)+"")+"'";
|
|
|
|
387
|
+ }
|
|
|
|
388
|
+ return sql;
|
|
|
|
389
|
+ }
|
|
|
|
390
|
+
|
|
|
|
391
|
+ /**
|
|
|
|
392
|
+ * 通过id删除数据
|
|
|
|
393
|
+ * @param para
|
|
|
|
394
|
+ * @return
|
|
|
|
395
|
+ */
|
|
|
|
396
|
+ public String deleteObjectById(Map<String, Object> para)
|
|
|
|
397
|
+ {
|
|
|
|
398
|
+ Class<?> objectCalss = (Class<?>) para.get("objectCalss");
|
|
|
|
399
|
+ String tableName = null;
|
|
|
|
400
|
+ if(para.containsKey("tableName"))
|
|
|
|
401
|
+ {
|
|
|
|
402
|
+ tableName = (String) para.get("tableName");
|
|
|
|
403
|
+ }
|
|
|
|
404
|
+
|
|
|
|
405
|
+ String id = (String) para.get("id");
|
|
|
|
406
|
+ if(StringUtils.isBlank(tableName))
|
|
|
|
407
|
+ {
|
|
|
|
408
|
+ tableName = Sqlutil.toUnderScoreCase(objectCalss.getSimpleName());
|
|
|
|
409
|
+ }
|
|
|
|
410
|
+ String sql = "delete from ";
|
|
|
|
411
|
+ sql += tableName + " where 1=1 ";
|
|
|
|
412
|
+ sql += " and "+"`id`"+"='"+escapeSql(id)+"'";
|
|
|
|
413
|
+ return sql;
|
|
|
|
414
|
+ }
|
|
|
|
415
|
+
|
|
|
|
416
|
+ /**
|
|
|
|
417
|
+ * 获取对象列表总数
|
|
|
|
418
|
+ */
|
|
|
|
419
|
+ public String getObjectListTotle(Map<String, Object> para)
|
|
|
|
420
|
+ {
|
|
|
|
421
|
+ Object object = para.get("object");
|
|
|
|
422
|
+ @SuppressWarnings("unchecked")
|
|
|
|
423
|
+ Map<String,String> whereMap = (Map<String, String>) para.get("whereMap");
|
|
|
|
424
|
+
|
|
|
|
425
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
426
|
+ String sql = "select count(*) from "+tableName;
|
|
|
|
427
|
+ String where = " where 1=1 ";
|
|
|
|
428
|
+
|
|
|
|
429
|
+ String like = "";
|
|
|
|
430
|
+
|
|
|
|
431
|
+ Field[] fields = object.getClass().getDeclaredFields();
|
|
|
|
432
|
+ Method[] methods = object.getClass().getMethods();
|
|
|
|
433
|
+ if(null != fields && fields.length !=0 )
|
|
|
|
434
|
+ {
|
|
|
|
435
|
+ for(Field field:fields)
|
|
|
|
436
|
+ {
|
|
|
|
437
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
438
|
+ {
|
|
|
|
439
|
+ continue;
|
|
|
|
440
|
+ }
|
|
|
|
441
|
+ try {
|
|
|
|
442
|
+ Method method;
|
|
|
|
443
|
+ method = object.getClass().getMethod("get"+ Sqlutil.getName(field.getName()));
|
|
|
|
444
|
+ Object value = method.invoke(object);
|
|
|
|
445
|
+ if(null != value)
|
|
|
|
446
|
+ {
|
|
|
|
447
|
+ String orther = "";
|
|
|
|
448
|
+ String s = "=";
|
|
|
|
449
|
+ if(null != whereMap && null != whereMap.get(field.getName()))
|
|
|
|
450
|
+ {
|
|
|
|
451
|
+ s = whereMap.get(field.getName());
|
|
|
|
452
|
+ if("like".equals(s))
|
|
|
|
453
|
+ {
|
|
|
|
454
|
+ value = "%"+value+"%";
|
|
|
|
455
|
+ like += " or " + "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" '"+escapeSql(value+"")+"'"+orther ;
|
|
|
|
456
|
+ continue;
|
|
|
|
457
|
+ }
|
|
|
|
458
|
+ if("time".equals(s))
|
|
|
|
459
|
+ {
|
|
|
|
460
|
+ s = ">";
|
|
|
|
461
|
+ orther = " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`< '"+whereMap.get("end_"+field.getName())+"'";
|
|
|
|
462
|
+ }
|
|
|
|
463
|
+ if("in".equals(s))
|
|
|
|
464
|
+ {
|
|
|
|
465
|
+ s = "in";
|
|
|
|
466
|
+ value = (value+"").replace(",","','");
|
|
|
|
467
|
+ where += " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" ("+"'"+value+"'"+")"+orther;
|
|
|
|
468
|
+ continue;
|
|
|
|
469
|
+ }
|
|
|
|
470
|
+ }
|
|
|
|
471
|
+ where += " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" '"+escapeSql(value+"")+"'"+orther;
|
|
|
|
472
|
+ }
|
|
|
|
473
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
474
|
+ e.printStackTrace();
|
|
|
|
475
|
+ } catch (SecurityException e) {
|
|
|
|
476
|
+ // TODO Auto-generated catch block
|
|
|
|
477
|
+ e.printStackTrace();
|
|
|
|
478
|
+ } catch (IllegalAccessException e) {
|
|
|
|
479
|
+ // TODO Auto-generated catch block
|
|
|
|
480
|
+ e.printStackTrace();
|
|
|
|
481
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
482
|
+ // TODO Auto-generated catch block
|
|
|
|
483
|
+ e.printStackTrace();
|
|
|
|
484
|
+ } catch (InvocationTargetException e) {
|
|
|
|
485
|
+ // TODO Auto-generated catch block
|
|
|
|
486
|
+ e.printStackTrace();
|
|
|
|
487
|
+ }
|
|
|
|
488
|
+
|
|
|
|
489
|
+ }
|
|
|
|
490
|
+ }
|
|
|
|
491
|
+ sql += where;
|
|
|
|
492
|
+ if(StringUtils.isNoneBlank(like))
|
|
|
|
493
|
+ {
|
|
|
|
494
|
+ sql += " and (1=2 "+like+")";
|
|
|
|
495
|
+ }
|
|
|
|
496
|
+ return sql;
|
|
|
|
497
|
+ }
|
|
|
|
498
|
+
|
|
|
|
499
|
+ /**
|
|
|
|
500
|
+ * 获取对象列表
|
|
|
|
501
|
+ */
|
|
|
|
502
|
+ public String getObjectList(Map<String, Object> para)
|
|
|
|
503
|
+ {
|
|
|
|
504
|
+ Object object = para.get("object");
|
|
|
|
505
|
+ @SuppressWarnings("unchecked")
|
|
|
|
506
|
+ Map<String,String> whereMap = (Map<String, String>) para.get("whereMap");
|
|
|
|
507
|
+ String selectStr = " * ";
|
|
|
|
508
|
+ String order = "";
|
|
|
|
509
|
+ if(para.containsKey("order") && null != para.get("order"))
|
|
|
|
510
|
+ {
|
|
|
|
511
|
+ order = para.get("order") +"";
|
|
|
|
512
|
+ }
|
|
|
|
513
|
+ Integer pageSize = 0;
|
|
|
|
514
|
+ if(para.containsKey("pageSize") && null != para.get("pageSize"))
|
|
|
|
515
|
+ {
|
|
|
|
516
|
+ pageSize = (Integer) para.get("pageSize");
|
|
|
|
517
|
+ }
|
|
|
|
518
|
+ Integer pageNo = 0;
|
|
|
|
519
|
+ if(para.containsKey("pageNo") && null != para.get("pageNo"))
|
|
|
|
520
|
+ {
|
|
|
|
521
|
+ pageNo = (Integer) para.get("pageNo");
|
|
|
|
522
|
+ }
|
|
|
|
523
|
+ if(para.containsKey("selectStr") && null != para.get("selectStr"))
|
|
|
|
524
|
+ {
|
|
|
|
525
|
+ selectStr = para.get("selectStr")+"";
|
|
|
|
526
|
+ }else{
|
|
|
|
527
|
+ selectStr = getSelect(object.getClass());
|
|
|
|
528
|
+ }
|
|
|
|
529
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
530
|
+
|
|
|
|
531
|
+ if(para.containsKey("tableName") && null != para.get("tableName"))
|
|
|
|
532
|
+ {
|
|
|
|
533
|
+ tableName = para.get("tableName")+"";
|
|
|
|
534
|
+ }
|
|
|
|
535
|
+ String sql = "select "+selectStr+" from "+tableName;
|
|
|
|
536
|
+ String where = " where 1=1 ";
|
|
|
|
537
|
+ String like = "";
|
|
|
|
538
|
+
|
|
|
|
539
|
+ Method[] methods = object.getClass().getMethods();
|
|
|
|
540
|
+ Field[] fields = object.getClass().getDeclaredFields();
|
|
|
|
541
|
+ if(null != fields && fields.length !=0 )
|
|
|
|
542
|
+ {
|
|
|
|
543
|
+ for(int i=0;i<fields.length;i++)
|
|
|
|
544
|
+ {
|
|
|
|
545
|
+ Field field = fields[i];
|
|
|
|
546
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
547
|
+ {
|
|
|
|
548
|
+ continue;
|
|
|
|
549
|
+ }
|
|
|
|
550
|
+ try {
|
|
|
|
551
|
+ Method method;
|
|
|
|
552
|
+ method = object.getClass().getMethod("get"+ Sqlutil.getName(field.getName()));
|
|
|
|
553
|
+ Object value = method.invoke(object);
|
|
|
|
554
|
+ if(!(null == value))
|
|
|
|
555
|
+ {
|
|
|
|
556
|
+ String orther = "";
|
|
|
|
557
|
+ String s = "=";
|
|
|
|
558
|
+ if(!(null == whereMap || null == whereMap.get(field.getName())))
|
|
|
|
559
|
+ {
|
|
|
|
560
|
+ s = whereMap.get(field.getName());
|
|
|
|
561
|
+ if("like".equals(s))
|
|
|
|
562
|
+ {
|
|
|
|
563
|
+ value = "%"+value+"%";
|
|
|
|
564
|
+ like += " or " + "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" '"+escapeSql(value+"")+"'"+orther ;
|
|
|
|
565
|
+ continue;
|
|
|
|
566
|
+ }
|
|
|
|
567
|
+ if("time".equals(s))
|
|
|
|
568
|
+ {
|
|
|
|
569
|
+ s = ">";
|
|
|
|
570
|
+ orther = " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`< '"+whereMap.get("end_"+field.getName())+"'";
|
|
|
|
571
|
+ }
|
|
|
|
572
|
+ if("in".equals(s))
|
|
|
|
573
|
+ {
|
|
|
|
574
|
+ s = "in";
|
|
|
|
575
|
+ value = (value+"").replace(",","','");
|
|
|
|
576
|
+ where += " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" ("+"'"+value+"'"+")"+orther;
|
|
|
|
577
|
+ continue;
|
|
|
|
578
|
+ }
|
|
|
|
579
|
+ }
|
|
|
|
580
|
+ where += " and `"+Sqlutil.toUnderScoreCase(field.getName())+"`"+s+" '"+escapeSql(value+"")+"'"+orther;
|
|
|
|
581
|
+ }
|
|
|
|
582
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
583
|
+ e.printStackTrace();
|
|
|
|
584
|
+ } catch (SecurityException e) {
|
|
|
|
585
|
+ // TODO Auto-generated catch block
|
|
|
|
586
|
+ e.printStackTrace();
|
|
|
|
587
|
+ } catch (IllegalAccessException e) {
|
|
|
|
588
|
+ // TODO Auto-generated catch block
|
|
|
|
589
|
+ e.printStackTrace();
|
|
|
|
590
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
591
|
+ // TODO Auto-generated catch block
|
|
|
|
592
|
+ e.printStackTrace();
|
|
|
|
593
|
+ } catch (InvocationTargetException e) {
|
|
|
|
594
|
+ // TODO Auto-generated catch block
|
|
|
|
595
|
+ e.printStackTrace();
|
|
|
|
596
|
+ }
|
|
|
|
597
|
+
|
|
|
|
598
|
+ }
|
|
|
|
599
|
+ }
|
|
|
|
600
|
+ sql += where;
|
|
|
|
601
|
+ if(StringUtils.isNoneBlank(like))
|
|
|
|
602
|
+ {
|
|
|
|
603
|
+ sql += " and (1=2 "+like+")";
|
|
|
|
604
|
+ }
|
|
|
|
605
|
+ if(StringUtils.isNotBlank(order))
|
|
|
|
606
|
+ {
|
|
|
|
607
|
+ sql += " order by "+order;
|
|
|
|
608
|
+ }
|
|
|
|
609
|
+ if(0 != pageSize && 0 != pageNo)
|
|
|
|
610
|
+ {
|
|
|
|
611
|
+ sql += " limit "+((pageNo-1)*pageSize)+","+pageSize;
|
|
|
|
612
|
+ }
|
|
|
|
613
|
+ return sql;
|
|
|
|
614
|
+ }
|
|
|
|
615
|
+
|
|
|
|
616
|
+ /**
|
|
|
|
617
|
+ * 添加或更新对象
|
|
|
|
618
|
+ * INSERT INTO test(`in1`,`str1`) VALUES ('1','1');
|
|
|
|
619
|
+ * @param object 对象
|
|
|
|
620
|
+ * @return
|
|
|
|
621
|
+ */
|
|
|
|
622
|
+ public String saveOrUpdateObject(Object object)
|
|
|
|
623
|
+ {
|
|
|
|
624
|
+ String sql = "insert into ";
|
|
|
|
625
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
626
|
+ sql += tableName + "(";
|
|
|
|
627
|
+ Field[] fields = object.getClass().getDeclaredFields( );
|
|
|
|
628
|
+ Method[] methods = object.getClass().getMethods( );
|
|
|
|
629
|
+ String values = "(";
|
|
|
|
630
|
+ String update = "";
|
|
|
|
631
|
+ for(Field field:fields)
|
|
|
|
632
|
+ {//
|
|
|
|
633
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
634
|
+ {
|
|
|
|
635
|
+ continue;
|
|
|
|
636
|
+ }
|
|
|
|
637
|
+ Method method;
|
|
|
|
638
|
+ try {
|
|
|
|
639
|
+ method = object.getClass().getMethod("get"+Sqlutil.getName(field.getName()));
|
|
|
|
640
|
+ Object value = method.invoke(object);
|
|
|
|
641
|
+ if(null != value)
|
|
|
|
642
|
+ {
|
|
|
|
643
|
+ if(!"(".equals(values) )
|
|
|
|
644
|
+ {
|
|
|
|
645
|
+ sql += ",";
|
|
|
|
646
|
+ values += ",";
|
|
|
|
647
|
+ update += ",";
|
|
|
|
648
|
+ }
|
|
|
|
649
|
+ sql += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`";
|
|
|
|
650
|
+ values += "'"+ escapeSql(value+"")+"'";
|
|
|
|
651
|
+ update += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+"=VALUES("+"`"+Sqlutil.toUnderScoreCase(field.getName())+"`)";
|
|
|
|
652
|
+ }
|
|
|
|
653
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
654
|
+ // TODO Auto-generated catch block
|
|
|
|
655
|
+ e.printStackTrace();
|
|
|
|
656
|
+ } catch (SecurityException e) {
|
|
|
|
657
|
+ // TODO Auto-generated catch block
|
|
|
|
658
|
+ e.printStackTrace();
|
|
|
|
659
|
+ } catch (IllegalAccessException e) {
|
|
|
|
660
|
+ // TODO Auto-generated catch block
|
|
|
|
661
|
+ e.printStackTrace();
|
|
|
|
662
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
663
|
+ // TODO Auto-generated catch block
|
|
|
|
664
|
+ e.printStackTrace();
|
|
|
|
665
|
+ } catch (InvocationTargetException e) {
|
|
|
|
666
|
+ // TODO Auto-generated catch block
|
|
|
|
667
|
+ e.printStackTrace();
|
|
|
|
668
|
+ }
|
|
|
|
669
|
+
|
|
|
|
670
|
+
|
|
|
|
671
|
+ }
|
|
|
|
672
|
+ sql += ")";
|
|
|
|
673
|
+ values += ")";
|
|
|
|
674
|
+ return sql+" values "+values+" ON DUPLICATE KEY UPDATE "+update;
|
|
|
|
675
|
+ }
|
|
|
|
676
|
+
|
|
|
|
677
|
+ /**
|
|
|
|
678
|
+ * 添加或更新对象列表
|
|
|
|
679
|
+ * INSERT INTO `test` (`in1`,`str1`)VALUES ('1','2'),('2','2') ON DUPLICATE KEY UPDATE `in1`=VALUES(`in1`),`str1`=VALUES(`str1`);
|
|
|
|
680
|
+ * @param objectlist 对象列表
|
|
|
|
681
|
+ * @return
|
|
|
|
682
|
+ */
|
|
|
|
683
|
+ public String saveOrUpdateObjectList(List<Object> objectlist)
|
|
|
|
684
|
+ {
|
|
|
|
685
|
+ StringBuffer sb =new StringBuffer();
|
|
|
|
686
|
+ String update = "";
|
|
|
|
687
|
+ for(int i = 0; i<objectlist.size();i++)
|
|
|
|
688
|
+ {
|
|
|
|
689
|
+ Object object = objectlist.get(i);
|
|
|
|
690
|
+
|
|
|
|
691
|
+ Field[] fields = object.getClass().getDeclaredFields( );
|
|
|
|
692
|
+ Method[] methods = object.getClass().getMethods( );
|
|
|
|
693
|
+ if(i==0)
|
|
|
|
694
|
+ {
|
|
|
|
695
|
+ String tableName = changTableNameFromObject(object);
|
|
|
|
696
|
+ sb.append("INSERT INTO `"+tableName+"` ");
|
|
|
|
697
|
+ sb.append("(");
|
|
|
|
698
|
+ for(Field field:fields)
|
|
|
|
699
|
+ {
|
|
|
|
700
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
701
|
+ {
|
|
|
|
702
|
+ continue;
|
|
|
|
703
|
+ }
|
|
|
|
704
|
+ if(!"".equals(update) )
|
|
|
|
705
|
+ {
|
|
|
|
706
|
+ sb.append(",");
|
|
|
|
707
|
+ update += ",";
|
|
|
|
708
|
+ }
|
|
|
|
709
|
+ sb.append("`"+Sqlutil.toUnderScoreCase(field.getName())+"`");
|
|
|
|
710
|
+ update += "`"+Sqlutil.toUnderScoreCase(field.getName())+"`"+"=VALUES("+"`"+Sqlutil.toUnderScoreCase(field.getName())+"`)";
|
|
|
|
711
|
+ }
|
|
|
|
712
|
+ sb.append(")");
|
|
|
|
713
|
+ sb.append("VALUES ");
|
|
|
|
714
|
+ }else{
|
|
|
|
715
|
+ sb.append(",");
|
|
|
|
716
|
+ }
|
|
|
|
717
|
+ for(int j=0;j<fields.length;j++)
|
|
|
|
718
|
+ {
|
|
|
|
719
|
+ Field field = fields[j];
|
|
|
|
720
|
+ Method method;
|
|
|
|
721
|
+ try {
|
|
|
|
722
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
723
|
+ {
|
|
|
|
724
|
+ continue;
|
|
|
|
725
|
+ }
|
|
|
|
726
|
+ method = object.getClass().getMethod("get"+Sqlutil.getName(field.getName()));
|
|
|
|
727
|
+ Object value = method.invoke(object);
|
|
|
|
728
|
+ if(null == value)
|
|
|
|
729
|
+ {
|
|
|
|
730
|
+ value = "";
|
|
|
|
731
|
+ }
|
|
|
|
732
|
+ if(j!=0)
|
|
|
|
733
|
+ {
|
|
|
|
734
|
+ sb.append(",");
|
|
|
|
735
|
+ }else{
|
|
|
|
736
|
+ sb.append("(");
|
|
|
|
737
|
+ }
|
|
|
|
738
|
+ sb.append("'"+ escapeSql(value+"")+"'");
|
|
|
|
739
|
+ if(j==fields.length-1)
|
|
|
|
740
|
+ {
|
|
|
|
741
|
+ sb.append(")");
|
|
|
|
742
|
+ }
|
|
|
|
743
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
744
|
+ // TODO Auto-generated catch block
|
|
|
|
745
|
+ e.printStackTrace();
|
|
|
|
746
|
+ } catch (SecurityException e) {
|
|
|
|
747
|
+ // TODO Auto-generated catch block
|
|
|
|
748
|
+ e.printStackTrace();
|
|
|
|
749
|
+ } catch (IllegalAccessException e) {
|
|
|
|
750
|
+ // TODO Auto-generated catch block
|
|
|
|
751
|
+ e.printStackTrace();
|
|
|
|
752
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
753
|
+ // TODO Auto-generated catch block
|
|
|
|
754
|
+ e.printStackTrace();
|
|
|
|
755
|
+ } catch (InvocationTargetException e) {
|
|
|
|
756
|
+ // TODO Auto-generated catch block
|
|
|
|
757
|
+ e.printStackTrace();
|
|
|
|
758
|
+ }
|
|
|
|
759
|
+
|
|
|
|
760
|
+ }
|
|
|
|
761
|
+ }
|
|
|
|
762
|
+ sb.append(" ON DUPLICATE KEY UPDATE ");
|
|
|
|
763
|
+ sb.append(update);
|
|
|
|
764
|
+ return sb.toString();
|
|
|
|
765
|
+ }
|
|
|
|
766
|
+
|
|
|
|
767
|
+ public String getObjectListBySQL(Map<String, Object> para)
|
|
|
|
768
|
+ {
|
|
|
|
769
|
+ return para.get("sql")+"";
|
|
|
|
770
|
+ }
|
|
|
|
771
|
+
|
|
|
|
772
|
+ public String updateBySql(String sql)
|
|
|
|
773
|
+ {
|
|
|
|
774
|
+ return sql;
|
|
|
|
775
|
+ }
|
|
|
|
776
|
+
|
|
|
|
777
|
+ public String selectCountBySql(Map<String, Object> para)
|
|
|
|
778
|
+ {
|
|
|
|
779
|
+ String sql = (String) para.get("sql");
|
|
|
|
780
|
+ if(!sql.toUpperCase().startsWith("SELECT COUNT("))
|
|
|
|
781
|
+ {
|
|
|
|
782
|
+ try {
|
|
|
|
783
|
+ throw new Exception("不是count查询");
|
|
|
|
784
|
+ } catch (Exception e) {
|
|
|
|
785
|
+ e.printStackTrace();
|
|
|
|
786
|
+ }
|
|
|
|
787
|
+ }
|
|
|
|
788
|
+ return sql;
|
|
|
|
789
|
+ }
|
|
|
|
790
|
+
|
|
|
|
791
|
+ public static String escapeSql(String str) {
|
|
|
|
792
|
+ return str == null ? null : StringUtils.replace(str, "'", "''");
|
|
|
|
793
|
+ }
|
|
|
|
794
|
+
|
|
|
|
795
|
+ private String getSelect(Class<?> clas)
|
|
|
|
796
|
+ {
|
|
|
|
797
|
+ String select = "";
|
|
|
|
798
|
+ Field[] fields = clas.getDeclaredFields();
|
|
|
|
799
|
+ Method[] methods = clas.getMethods();
|
|
|
|
800
|
+ for(Field field:fields)
|
|
|
|
801
|
+ {
|
|
|
|
802
|
+ if(!haveGetMethod(methods,field))
|
|
|
|
803
|
+ {
|
|
|
|
804
|
+ continue;
|
|
|
|
805
|
+ }
|
|
|
|
806
|
+ if(!"".equals(select))
|
|
|
|
807
|
+ {
|
|
|
|
808
|
+ select += ",";
|
|
|
|
809
|
+ }
|
|
|
|
810
|
+ String fieldName = field.getName();
|
|
|
|
811
|
+ select +="`"+ Sqlutil.toUnderScoreCase(fieldName) +"` "+fieldName;
|
|
|
|
812
|
+ }
|
|
|
|
813
|
+ if("".equals(select))
|
|
|
|
814
|
+ {
|
|
|
|
815
|
+ select = "*";
|
|
|
|
816
|
+ }
|
|
|
|
817
|
+ return select;
|
|
|
|
818
|
+ }
|
|
|
|
819
|
+
|
|
|
|
820
|
+
|
|
|
|
821
|
+ public <T> String selectTList(T t)
|
|
|
|
822
|
+ {
|
|
|
|
823
|
+ String tableName = changTableNameFromObject(t);
|
|
|
|
824
|
+ StringBuffer stringBuffer = new StringBuffer("select * from ");
|
|
|
|
825
|
+ stringBuffer.append("`");
|
|
|
|
826
|
+ stringBuffer.append(tableName);
|
|
|
|
827
|
+ stringBuffer.append("`");
|
|
|
|
828
|
+
|
|
|
|
829
|
+ StringBuffer orderbyStringBuffer = new StringBuffer();
|
|
|
|
830
|
+
|
|
|
|
831
|
+ StringBuffer whereStringBuffer = new StringBuffer();
|
|
|
|
832
|
+ if(t instanceof MyBaseEntity)
|
|
|
|
833
|
+ {
|
|
|
|
834
|
+ MyBaseEntity baseEntity = (MyBaseEntity) t;
|
|
|
|
835
|
+ Map<String, Object[]> timeMap = baseEntity.getTimeMap();
|
|
|
|
836
|
+ String[] keyValues = baseEntity.getKeyValue();
|
|
|
|
837
|
+ Map<String, QueryType> params = baseEntity.getParams();
|
|
|
|
838
|
+
|
|
|
|
839
|
+ String orderBy = baseEntity.getOrderBy();
|
|
|
|
840
|
+
|
|
|
|
841
|
+ if(StringUtils.isNotEmpty(timeMap)) //时间条件
|
|
|
|
842
|
+ {
|
|
|
|
843
|
+ for (String time:timeMap.keySet())
|
|
|
|
844
|
+ {
|
|
|
|
845
|
+ whereStringBuffer.append(" AND ");
|
|
|
|
846
|
+ whereStringBuffer.append(setObject(time,QueryType.GTE,timeMap.get(time)[0]));// 开始时间
|
|
|
|
847
|
+ whereStringBuffer.append(" AND ");
|
|
|
|
848
|
+ whereStringBuffer.append(setObject(time,QueryType.LTE,timeMap.get(time)[1]));// 结束时间
|
|
|
|
849
|
+
|
|
|
|
850
|
+ }
|
|
|
|
851
|
+ }
|
|
|
|
852
|
+
|
|
|
|
853
|
+
|
|
|
|
854
|
+ if(StringUtils.isNotEmpty(params)) //自定义条件
|
|
|
|
855
|
+ {
|
|
|
|
856
|
+ Class clas = t.getClass();
|
|
|
|
857
|
+ for (String key:params.keySet())
|
|
|
|
858
|
+ {
|
|
|
|
859
|
+ String getterMethodName = "get"+ org.apache.commons.lang3.StringUtils.capitalize(key);
|
|
|
|
860
|
+ try {
|
|
|
|
861
|
+ Method method = clas.getMethod(getterMethodName);
|
|
|
|
862
|
+ Object value = method.invoke(t);
|
|
|
|
863
|
+ if(StringUtils.isNotNull(value))
|
|
|
|
864
|
+ {
|
|
|
|
865
|
+ QueryType queryType = params.get(key);
|
|
|
|
866
|
+ whereStringBuffer.append(" AND ");
|
|
|
|
867
|
+ whereStringBuffer.append(setObject(key,queryType,value));
|
|
|
|
868
|
+ }
|
|
|
|
869
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
870
|
+ throw new RuntimeException(e);
|
|
|
|
871
|
+ } catch (InvocationTargetException e) {
|
|
|
|
872
|
+ throw new RuntimeException(e);
|
|
|
|
873
|
+ } catch (IllegalAccessException e) {
|
|
|
|
874
|
+ throw new RuntimeException(e);
|
|
|
|
875
|
+ }
|
|
|
|
876
|
+
|
|
|
|
877
|
+
|
|
|
|
878
|
+ }
|
|
|
|
879
|
+ }
|
|
|
|
880
|
+
|
|
|
|
881
|
+ if(StringUtils.isNotEmpty(keyValues)) //模糊匹配添加
|
|
|
|
882
|
+ {
|
|
|
|
883
|
+ StringBuffer likeString = new StringBuffer();
|
|
|
|
884
|
+ String value = keyValues[0];
|
|
|
|
885
|
+ String[] keys = keyValues[1].split(",");
|
|
|
|
886
|
+ for(String key:keys)
|
|
|
|
887
|
+ {
|
|
|
|
888
|
+ likeString.append(" OR ");
|
|
|
|
889
|
+ likeString.append(setObject(key,QueryType.LIKE,value));
|
|
|
|
890
|
+
|
|
|
|
891
|
+ }
|
|
|
|
892
|
+ if(likeString.length() >0)
|
|
|
|
893
|
+ {
|
|
|
|
894
|
+ whereStringBuffer.append(" AND (");
|
|
|
|
895
|
+ whereStringBuffer.append(likeString.substring(3));
|
|
|
|
896
|
+ whereStringBuffer.append(")");
|
|
|
|
897
|
+ }
|
|
|
|
898
|
+
|
|
|
|
899
|
+ }
|
|
|
|
900
|
+
|
|
|
|
901
|
+ if(StringUtils.isNotEmpty(orderBy)) //排序
|
|
|
|
902
|
+ {
|
|
|
|
903
|
+ orderbyStringBuffer.append(" ORDER BY ");
|
|
|
|
904
|
+ orderbyStringBuffer.append(orderBy);
|
|
|
|
905
|
+ }
|
|
|
|
906
|
+ }
|
|
|
|
907
|
+
|
|
|
|
908
|
+ getValuseWhere(t,whereStringBuffer); //不带条件的有值参数
|
|
|
|
909
|
+
|
|
|
|
910
|
+ if(whereStringBuffer.length()>0)
|
|
|
|
911
|
+ {
|
|
|
|
912
|
+ stringBuffer.append(" WHERE ").append(whereStringBuffer.substring(4));
|
|
|
|
913
|
+ }
|
|
|
|
914
|
+
|
|
|
|
915
|
+ if(orderbyStringBuffer.length()>0)
|
|
|
|
916
|
+ {
|
|
|
|
917
|
+ stringBuffer.append(orderbyStringBuffer);
|
|
|
|
918
|
+ }
|
|
|
|
919
|
+
|
|
|
|
920
|
+ return stringBuffer.toString();
|
|
|
|
921
|
+ }
|
|
|
|
922
|
+
|
|
|
|
923
|
+ private String setObject(String key,QueryType queryType,Object o)
|
|
|
|
924
|
+ {
|
|
|
|
925
|
+ String dengshi = "=";
|
|
|
|
926
|
+ String yingyong = "'{}'";
|
|
|
|
927
|
+ switch (queryType)
|
|
|
|
928
|
+ {
|
|
|
|
929
|
+ case EQ:
|
|
|
|
930
|
+ dengshi = " = ";
|
|
|
|
931
|
+ break;
|
|
|
|
932
|
+ case NE:
|
|
|
|
933
|
+ dengshi = " != ";
|
|
|
|
934
|
+ break;
|
|
|
|
935
|
+ case GT:
|
|
|
|
936
|
+ dengshi = " > ";
|
|
|
|
937
|
+ break;
|
|
|
|
938
|
+ case GTE:
|
|
|
|
939
|
+ dengshi = " >= ";
|
|
|
|
940
|
+ break;
|
|
|
|
941
|
+ case LT:
|
|
|
|
942
|
+ dengshi = " < ";
|
|
|
|
943
|
+ break;
|
|
|
|
944
|
+ case LTE:
|
|
|
|
945
|
+ dengshi = " <= ";
|
|
|
|
946
|
+ break;
|
|
|
|
947
|
+ case LIKE:
|
|
|
|
948
|
+ dengshi = " like ";
|
|
|
|
949
|
+ yingyong = "'%{}%'";
|
|
|
|
950
|
+ break;
|
|
|
|
951
|
+ }
|
|
|
|
952
|
+
|
|
|
|
953
|
+ if(o instanceof Number)
|
|
|
|
954
|
+ {
|
|
|
|
955
|
+ yingyong = "{}";
|
|
|
|
956
|
+ }else if(o instanceof Date)
|
|
|
|
957
|
+ {
|
|
|
|
958
|
+ yingyong = "'"+ DateUtils.parseDateToStr(DateUtils.getTime(), (Date) o)+"'";
|
|
|
|
959
|
+ }
|
|
|
|
960
|
+
|
|
|
|
961
|
+ return " `"+Sqlutil.toUnderScoreCase(key)+"`"+dengshi+ StringUtils.format(yingyong,o) +" ";
|
|
|
|
962
|
+ }
|
|
|
|
963
|
+
|
|
|
|
964
|
+ private <T> void getValuseWhere(T t,StringBuffer stringBuffer)
|
|
|
|
965
|
+ {
|
|
|
|
966
|
+
|
|
|
|
967
|
+ Class clas = t.getClass();
|
|
|
|
968
|
+ Field[] fields = clas.getDeclaredFields(); //获取所有属性
|
|
|
|
969
|
+
|
|
|
|
970
|
+ Method[] methods = clas.getMethods(); //获取所有方法
|
|
|
|
971
|
+
|
|
|
|
972
|
+ for (Field field:fields)
|
|
|
|
973
|
+ {
|
|
|
|
974
|
+ field.setAccessible(true);
|
|
|
|
975
|
+ try {
|
|
|
|
976
|
+ Object value = field.get(t);
|
|
|
|
977
|
+ if(null != value && haveGetMethod(methods,field))
|
|
|
|
978
|
+ {
|
|
|
|
979
|
+ stringBuffer.append(" AND ");
|
|
|
|
980
|
+ stringBuffer.append(setObject(field.getName(),QueryType.EQ,value));
|
|
|
|
981
|
+ }
|
|
|
|
982
|
+ } catch (IllegalAccessException e) {
|
|
|
|
983
|
+ e.printStackTrace();
|
|
|
|
984
|
+ }
|
|
|
|
985
|
+
|
|
|
|
986
|
+ }
|
|
|
|
987
|
+
|
|
|
|
988
|
+ }
|
|
|
|
989
|
+
|
|
|
|
990
|
+ private boolean haveGetMethod(Method[] methods,Field field)
|
|
|
|
991
|
+ {
|
|
|
|
992
|
+ for (Method method:methods) //如何没有get方法就过滤掉
|
|
|
|
993
|
+ {
|
|
|
|
994
|
+ if(method.getName().equals("get"+ org.apache.commons.lang3.StringUtils.capitalize(field.getName())))
|
|
|
|
995
|
+ {
|
|
|
|
996
|
+ return true;
|
|
|
|
997
|
+ }
|
|
|
|
998
|
+ }
|
|
|
|
999
|
+ return false;
|
|
|
|
1000
|
+ }
|
|
|
|
1001
|
+} |