Skip to content

Commit 84cf0de

Browse files
committed
BaseServiceImpl只捕获除方法外的异常,支持BaseService方法在事务内调用
1 parent 86e50ad commit 84cf0de

File tree

1 file changed

+106
-21
lines changed

1 file changed

+106
-21
lines changed

zheng-common/src/main/java/com/zheng/common/base/BaseServiceImpl.java

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.lang.StringUtils;
88
import org.apache.ibatis.annotations.Param;
99

10+
import java.lang.reflect.InvocationTargetException;
1011
import java.lang.reflect.Method;
1112
import java.lang.reflect.ParameterizedType;
1213
import java.util.List;
@@ -26,7 +27,11 @@ public int countByExample(Example example) {
2627
Method countByExample = mapper.getClass().getDeclaredMethod("countByExample", example.getClass());
2728
Object result = countByExample.invoke(mapper, example);
2829
return Integer.parseInt(String.valueOf(result));
29-
} catch (Exception e) {
30+
} catch (IllegalAccessException e) {
31+
e.printStackTrace();
32+
} catch (InvocationTargetException e) {
33+
e.printStackTrace();
34+
} catch (NoSuchMethodException e) {
3035
e.printStackTrace();
3136
}
3237
DynamicDataSource.clearDataSource();
@@ -40,7 +45,11 @@ public int deleteByExample(Example example) {
4045
Method deleteByExample = mapper.getClass().getDeclaredMethod("deleteByExample", example.getClass());
4146
Object result = deleteByExample.invoke(mapper, example);
4247
return Integer.parseInt(String.valueOf(result));
43-
} catch (Exception e) {
48+
} catch (IllegalAccessException e) {
49+
e.printStackTrace();
50+
} catch (InvocationTargetException e) {
51+
e.printStackTrace();
52+
} catch (NoSuchMethodException e) {
4453
e.printStackTrace();
4554
}
4655
DynamicDataSource.clearDataSource();
@@ -54,7 +63,11 @@ public int deleteByPrimaryKey(Integer id) {
5463
Method deleteByPrimaryKey = mapper.getClass().getDeclaredMethod("deleteByPrimaryKey", id.getClass());
5564
Object result = deleteByPrimaryKey.invoke(mapper, id);
5665
return Integer.parseInt(String.valueOf(result));
57-
} catch (Exception e) {
66+
} catch (IllegalAccessException e) {
67+
e.printStackTrace();
68+
} catch (InvocationTargetException e) {
69+
e.printStackTrace();
70+
} catch (NoSuchMethodException e) {
5871
e.printStackTrace();
5972
}
6073
DynamicDataSource.clearDataSource();
@@ -68,7 +81,11 @@ public int insert(Record record) {
6881
Method insert = mapper.getClass().getDeclaredMethod("insert", record.getClass());
6982
Object result = insert.invoke(mapper, record);
7083
return Integer.parseInt(String.valueOf(result));
71-
} catch (Exception e) {
84+
} catch (IllegalAccessException e) {
85+
e.printStackTrace();
86+
} catch (InvocationTargetException e) {
87+
e.printStackTrace();
88+
} catch (NoSuchMethodException e) {
7289
e.printStackTrace();
7390
}
7491
DynamicDataSource.clearDataSource();
@@ -82,7 +99,11 @@ public int insertSelective(Record record) {
8299
Method insertSelective = mapper.getClass().getDeclaredMethod("insertSelective", record.getClass());
83100
Object result = insertSelective.invoke(mapper, record);
84101
return Integer.parseInt(String.valueOf(result));
85-
} catch (Exception e) {
102+
} catch (IllegalAccessException e) {
103+
e.printStackTrace();
104+
} catch (InvocationTargetException e) {
105+
e.printStackTrace();
106+
} catch (NoSuchMethodException e) {
86107
e.printStackTrace();
87108
}
88109
DynamicDataSource.clearDataSource();
@@ -96,7 +117,11 @@ public List<Record> selectByExampleWithBLOBs(Example example) {
96117
Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
97118
Object result = selectByExampleWithBLOBs.invoke(mapper, example);
98119
return (List<Record>) result;
99-
} catch (Exception e) {
120+
} catch (IllegalAccessException e) {
121+
e.printStackTrace();
122+
} catch (InvocationTargetException e) {
123+
e.printStackTrace();
124+
} catch (NoSuchMethodException e) {
100125
e.printStackTrace();
101126
}
102127
DynamicDataSource.clearDataSource();
@@ -110,7 +135,11 @@ public List<Record> selectByExample(Example example) {
110135
Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
111136
Object result = selectByExample.invoke(mapper, example);
112137
return (List<Record>) result;
113-
} catch (Exception e) {
138+
} catch (IllegalAccessException e) {
139+
e.printStackTrace();
140+
} catch (InvocationTargetException e) {
141+
e.printStackTrace();
142+
} catch (NoSuchMethodException e) {
114143
e.printStackTrace();
115144
}
116145
DynamicDataSource.clearDataSource();
@@ -125,7 +154,11 @@ public List<Record> selectByExampleWithBLOBsForStartPage(Example example, Intege
125154
PageHelper.startPage(pageNum, pageSize, false);
126155
Object result = selectByExampleWithBLOBs.invoke(mapper, example);
127156
return (List<Record>) result;
128-
} catch (Exception e) {
157+
} catch (IllegalAccessException e) {
158+
e.printStackTrace();
159+
} catch (InvocationTargetException e) {
160+
e.printStackTrace();
161+
} catch (NoSuchMethodException e) {
129162
e.printStackTrace();
130163
}
131164
DynamicDataSource.clearDataSource();
@@ -140,7 +173,11 @@ public List<Record> selectByExampleForStartPage(Example example, Integer pageNum
140173
PageHelper.startPage(pageNum, pageSize, false);
141174
Object result = selectByExample.invoke(mapper, example);
142175
return (List<Record>) result;
143-
} catch (Exception e) {
176+
} catch (IllegalAccessException e) {
177+
e.printStackTrace();
178+
} catch (InvocationTargetException e) {
179+
e.printStackTrace();
180+
} catch (NoSuchMethodException e) {
144181
e.printStackTrace();
145182
}
146183
DynamicDataSource.clearDataSource();
@@ -155,7 +192,11 @@ public List<Record> selectByExampleWithBLOBsForOffsetPage(Example example, Integ
155192
PageHelper.offsetPage(offset, limit, false);
156193
Object result = selectByExampleWithBLOBs.invoke(mapper, example);
157194
return (List<Record>) result;
158-
} catch (Exception e) {
195+
} catch (IllegalAccessException e) {
196+
e.printStackTrace();
197+
} catch (InvocationTargetException e) {
198+
e.printStackTrace();
199+
} catch (NoSuchMethodException e) {
159200
e.printStackTrace();
160201
}
161202
DynamicDataSource.clearDataSource();
@@ -170,7 +211,11 @@ public List<Record> selectByExampleForOffsetPage(Example example, Integer offset
170211
PageHelper.offsetPage(offset, limit, false);
171212
Object result = selectByExample.invoke(mapper, example);
172213
return (List<Record>) result;
173-
} catch (Exception e) {
214+
} catch (IllegalAccessException e) {
215+
e.printStackTrace();
216+
} catch (InvocationTargetException e) {
217+
e.printStackTrace();
218+
} catch (NoSuchMethodException e) {
174219
e.printStackTrace();
175220
}
176221
DynamicDataSource.clearDataSource();
@@ -186,7 +231,11 @@ public Record selectFirstByExample(Example example) {
186231
if (null != result && result.size() > 0) {
187232
return result.get(0);
188233
}
189-
} catch (Exception e) {
234+
} catch (IllegalAccessException e) {
235+
e.printStackTrace();
236+
} catch (InvocationTargetException e) {
237+
e.printStackTrace();
238+
} catch (NoSuchMethodException e) {
190239
e.printStackTrace();
191240
}
192241
DynamicDataSource.clearDataSource();
@@ -202,7 +251,11 @@ public Record selectFirstByExampleWithBLOBs(Example example) {
202251
if (null != result && result.size() > 0) {
203252
return result.get(0);
204253
}
205-
} catch (Exception e) {
254+
} catch (IllegalAccessException e) {
255+
e.printStackTrace();
256+
} catch (InvocationTargetException e) {
257+
e.printStackTrace();
258+
} catch (NoSuchMethodException e) {
206259
e.printStackTrace();
207260
}
208261
DynamicDataSource.clearDataSource();
@@ -216,7 +269,11 @@ public Record selectByPrimaryKey(Integer id) {
216269
Method selectByPrimaryKey = mapper.getClass().getDeclaredMethod("selectByPrimaryKey", id.getClass());
217270
Object result = selectByPrimaryKey.invoke(mapper, id);
218271
return (Record) result;
219-
} catch (Exception e) {
272+
} catch (IllegalAccessException e) {
273+
e.printStackTrace();
274+
} catch (InvocationTargetException e) {
275+
e.printStackTrace();
276+
} catch (NoSuchMethodException e) {
220277
e.printStackTrace();
221278
}
222279
DynamicDataSource.clearDataSource();
@@ -230,7 +287,11 @@ public int updateByExampleSelective(@Param("record") Record record, @Param("exam
230287
Method updateByExampleSelective = mapper.getClass().getDeclaredMethod("updateByExampleSelective", record.getClass(), example.getClass());
231288
Object result = updateByExampleSelective.invoke(mapper, record, example);
232289
return Integer.parseInt(String.valueOf(result));
233-
} catch (Exception e) {
290+
} catch (IllegalAccessException e) {
291+
e.printStackTrace();
292+
} catch (InvocationTargetException e) {
293+
e.printStackTrace();
294+
} catch (NoSuchMethodException e) {
234295
e.printStackTrace();
235296
}
236297
DynamicDataSource.clearDataSource();
@@ -244,7 +305,11 @@ public int updateByExampleWithBLOBs(@Param("record") Record record, @Param("exam
244305
Method updateByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("updateByExampleWithBLOBs", record.getClass(), example.getClass());
245306
Object result = updateByExampleWithBLOBs.invoke(mapper, record, example);
246307
return Integer.parseInt(String.valueOf(result));
247-
} catch (Exception e) {
308+
} catch (IllegalAccessException e) {
309+
e.printStackTrace();
310+
} catch (InvocationTargetException e) {
311+
e.printStackTrace();
312+
} catch (NoSuchMethodException e) {
248313
e.printStackTrace();
249314
}
250315
DynamicDataSource.clearDataSource();
@@ -258,7 +323,11 @@ public int updateByExample(@Param("record") Record record, @Param("example") Exa
258323
Method updateByExample = mapper.getClass().getDeclaredMethod("updateByExample", record.getClass(), example.getClass());
259324
Object result = updateByExample.invoke(mapper, record, example);
260325
return Integer.parseInt(String.valueOf(result));
261-
} catch (Exception e) {
326+
} catch (IllegalAccessException e) {
327+
e.printStackTrace();
328+
} catch (InvocationTargetException e) {
329+
e.printStackTrace();
330+
} catch (NoSuchMethodException e) {
262331
e.printStackTrace();
263332
}
264333
DynamicDataSource.clearDataSource();
@@ -272,7 +341,11 @@ public int updateByPrimaryKeySelective(Record record) {
272341
Method updateByPrimaryKeySelective = mapper.getClass().getDeclaredMethod("updateByPrimaryKeySelective", record.getClass());
273342
Object result = updateByPrimaryKeySelective.invoke(mapper, record);
274343
return Integer.parseInt(String.valueOf(result));
275-
} catch (Exception e) {
344+
} catch (IllegalAccessException e) {
345+
e.printStackTrace();
346+
} catch (InvocationTargetException e) {
347+
e.printStackTrace();
348+
} catch (NoSuchMethodException e) {
276349
e.printStackTrace();
277350
}
278351
DynamicDataSource.clearDataSource();
@@ -286,7 +359,11 @@ public int updateByPrimaryKeyWithBLOBs(Record record) {
286359
Method updateByPrimaryKeyWithBLOBs = mapper.getClass().getDeclaredMethod("updateByPrimaryKeyWithBLOBs", record.getClass());
287360
Object result = updateByPrimaryKeyWithBLOBs.invoke(mapper, record);
288361
return Integer.parseInt(String.valueOf(result));
289-
} catch (Exception e) {
362+
} catch (IllegalAccessException e) {
363+
e.printStackTrace();
364+
} catch (InvocationTargetException e) {
365+
e.printStackTrace();
366+
} catch (NoSuchMethodException e) {
290367
e.printStackTrace();
291368
}
292369
DynamicDataSource.clearDataSource();
@@ -300,7 +377,11 @@ public int updateByPrimaryKey(Record record) {
300377
Method updateByPrimaryKey = mapper.getClass().getDeclaredMethod("updateByPrimaryKey", record.getClass());
301378
Object result = updateByPrimaryKey.invoke(mapper, record);
302379
return Integer.parseInt(String.valueOf(result));
303-
} catch (Exception e) {
380+
} catch (IllegalAccessException e) {
381+
e.printStackTrace();
382+
} catch (InvocationTargetException e) {
383+
e.printStackTrace();
384+
} catch (NoSuchMethodException e) {
304385
e.printStackTrace();
305386
}
306387
DynamicDataSource.clearDataSource();
@@ -326,7 +407,11 @@ public int deleteByPrimaryKeys(String ids) {
326407
count += Integer.parseInt(String.valueOf(result));
327408
}
328409
return count;
329-
} catch (Exception e) {
410+
} catch (IllegalAccessException e) {
411+
e.printStackTrace();
412+
} catch (InvocationTargetException e) {
413+
e.printStackTrace();
414+
} catch (NoSuchMethodException e) {
330415
e.printStackTrace();
331416
}
332417
DynamicDataSource.clearDataSource();

0 commit comments

Comments
 (0)