@@ -70,119 +70,3 @@ public class CommonFieldSetterSqlSourceCustomize implements SqlSourceCustomize {
70
70
```
71
71
72
72
2 . 基于SPI机制,在classPath下创建` META-INF/services/io.mybatis.provider.SqlSourceCustomize ` 文件,文件内输入 ` io.mybatis.provider.SqlSourceCustomize ` 的实现类,如:` com.l1yp.mybatis.mapper.CommonFieldSetterSqlSourceCustomize `
73
-
74
- ### 1.2.2 基于mybatis原生拦截器
75
-
76
- > 拦截ParameterHandler的setParameters方法即可
77
-
78
- ``` java
79
- package org.apache.ibatis.executor.parameter ;
80
-
81
- import java.sql.PreparedStatement ;
82
- import java.sql.SQLException ;
83
-
84
- public interface ParameterHandler {
85
-
86
- Object getParameterObject ();
87
-
88
- void setParameters (PreparedStatement ps ) throws SQLException ;
89
-
90
- }
91
- ```
92
-
93
- > 实现方案
94
- ``` JAVA
95
- @Intercepts ({
96
- @Signature (
97
- type = ParameterHandler . class,
98
- method = " setParameters" ,
99
- args = { PreparedStatement . class }
100
- ),
101
- })
102
- @Slf4j
103
- @Component
104
- public class ParameterizeInterceptor implements Interceptor {
105
-
106
- private static final String CREATE_BY = " createBy" ;
107
- private static final String UPDATE_BY = " updateBy" ;
108
-
109
- private static final String CREATE_TIME = " createTime" ;
110
- private static final String UPDATE_TIME = " updateTime" ;
111
-
112
- @Override
113
- public Object intercept (Invocation invocation ) throws Throwable {
114
- // 获取this对象,可能是Proxy
115
- ParameterHandler parameterHandler = (ParameterHandler ) invocation. getTarget();
116
- if (Proxy . isProxyClass(parameterHandler. getClass())) {
117
- InvocationHandler invocationHandler = Proxy . getInvocationHandler(parameterHandler);
118
- /**
119
- * 由于Interceptor接口的plugin默认方法对目标对象进行包装代理。
120
- * 因此此处需要读取Plugin内的Target对象
121
- * default Object plugin(Object target) {
122
- * return Plugin.wrap(target, this);
123
- * }
124
- */
125
- if (invocationHandler instanceof Plugin ) {
126
- Field targetField = ReflectionUtils . findField(invocationHandler. getClass(), " target" );
127
- if (targetField != null ) {
128
- targetField. setAccessible(true );
129
- parameterHandler = (ParameterHandler ) targetField. get(invocationHandler);
130
- }
131
- }
132
- }
133
-
134
- // 获取MappedStatement对象,用于判断执行的SQL命令信息
135
- MappedStatement mappedStatement = getMappedStatement(parameterHandler);
136
- SqlCommandType sqlCommandType = mappedStatement. getSqlCommandType();
137
- // 获取当前参数对象
138
- Object parameterObject = parameterHandler. getParameterObject();
139
- if (sqlCommandType == SqlCommandType . INSERT || sqlCommandType == SqlCommandType . UPDATE ) {
140
- MetaObject metaObject = mappedStatement. getConfiguration(). newMetaObject(parameterObject);
141
- // AbstractModel是我项目的数据库实体的父类,内部含有createBy、createTime字段
142
- // TODO: 自行修改此处逻辑,如判断字段是否存在
143
- if (parameterObject instanceof AbstractModel<?,?> && sqlCommandType == SqlCommandType . INSERT ) {
144
- setCommonField(metaObject, CREATE_BY , LoginUtils . getLoginUserId());
145
- setCommonField(metaObject, CREATE_TIME , LocalDateTime . now());
146
- }
147
- if (sqlCommandType == SqlCommandType . UPDATE ) {
148
- if (parameterObject instanceof AbstractWithUpdateModel<?> ) {
149
- setCommonField(metaObject, UPDATE_BY , LoginUtils . getLoginUserId());
150
- setCommonField(metaObject, UPDATE_TIME , LocalDateTime . now());
151
- } else {
152
- // 处理非AbstractWithUpdateModel子类也需要自动填充的字段
153
- if (metaObject. hasSetter(UPDATE_BY )) {
154
- setCommonField(metaObject, UPDATE_BY , LoginUtils . getLoginUserId());
155
- }
156
- if (metaObject. hasSetter(UPDATE_TIME )) {
157
- setCommonField(metaObject, UPDATE_TIME , LoginUtils . getLoginUserId());
158
- }
159
- }
160
-
161
-
162
-
163
- }
164
-
165
- }
166
-
167
- return invocation. proceed();
168
- }
169
-
170
-
171
- private void setCommonField (MetaObject metaObject , String field , Object value ) {
172
- Object createBy = metaObject. getValue(field);
173
- if (createBy == null ) {
174
- metaObject. setValue(field, value);
175
- }
176
- }
177
-
178
-
179
- private MappedStatement getMappedStatement (ParameterHandler parameterHandler ) throws NoSuchFieldException , IllegalAccessException {
180
- Field mappedStatementField = parameterHandler. getClass(). getDeclaredField(" mappedStatement" );
181
- mappedStatementField. setAccessible(true );
182
- return (MappedStatement ) mappedStatementField. get(parameterHandler);
183
- }
184
-
185
- }
186
-
187
- ```
188
-
0 commit comments