該攔截器的作用:在進(jìn)行增加、修改等操作時(shí),給數(shù)據(jù)模型的一些通用操作屬性(如:創(chuàng)建人、創(chuàng)建時(shí)間、修改人、修改時(shí)間等)自動(dòng)賦值。
該實(shí)現(xiàn)是在DAO層攔截,即存入DB前最后一層。后經(jīng)分析,不是很合理,改為在service層攔截,用spring AOP來(lái)實(shí)現(xiàn)了,該代碼遂棄用。不過(guò)已經(jīng)測(cè)試可用,記錄備忘。
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
|
package com.development; import java.lang.reflect.InvocationTargetException; import java.util.Date; import java.util.Map; import java.util.Properties; import org.apache.commons.beanutils.BeanUtils; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; /** * 攔截器作用:給各實(shí)體對(duì)象在增加、修改時(shí),自動(dòng)添加操作屬性信息。 */ @Intercepts ({ @Signature (type = Executor. class , method = "update" , args = {MappedStatement. class , Object. class }) }) public class OpeInfoInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); System.out.println( "02 當(dāng)前線程ID:" +Thread.currentThread().getId()); //遍歷處理所有參數(shù),update方法有兩個(gè)參數(shù),參見(jiàn)Executor類(lèi)中的update()方法。 for ( int i= 0 ;i<args.length;i++) { Object arg=args[i]; String className=arg.getClass().getName(); System.out.println(i + " 參數(shù)類(lèi)型:" +className); //第一個(gè)參數(shù)處理。根據(jù)它判斷是否給“操作屬性”賦值。 if (arg instanceof MappedStatement) { //如果是第一個(gè)參數(shù) MappedStatement MappedStatement ms = (MappedStatement)arg; SqlCommandType sqlCommandType = ms.getSqlCommandType(); System.out.println( "操作類(lèi)型:" +sqlCommandType); if (sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE) { //如果是“增加”或“更新”操作,則繼續(xù)進(jìn)行默認(rèn)操作信息賦值。否則,則退出 continue ; } else { break ; } } //第二個(gè)參數(shù)處理。(只有第二個(gè)程序才能跑到這) if (arg instanceof Map) { //如果是map,有兩種情況:(1)使用@Param多參數(shù)傳入,由Mybatis包裝成map。(2)原始傳入Map System.out.println( "這是一個(gè)包裝過(guò)的類(lèi)型!" ); Map map=(Map)arg; for (Object obj : map.values()) { setProperty(obj); } } else { //原始參數(shù)傳入 setProperty(arg); } } return invocation.proceed(); } /** * 為對(duì)象的操作屬性賦值 * @param obj */ private void setProperty(Object obj) { try { //TODO: 根據(jù)需要,將相關(guān)屬性賦上默認(rèn)值 BeanUtils.setProperty(obj, "createrUsername" , "張三" ); BeanUtils.setProperty(obj, "createDT" , new Date()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public Object plugin(Object target) { return Plugin.wrap(target, this ); } public void setProperties(Properties properties) { } } |
以上這篇MyBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。