MybatisInterceptor.java
6.4 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.skua.config.mybatis;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
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;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import com.skua.core.api.vo.LoginUser;
import com.skua.core.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
/**
* mybatis拦截器,自动注入创建人、创建时间、修改人、修改时间
*/
@Slf4j
@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class})})
public class MybatisInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sqlId = mappedStatement.getId();
log.debug("------sqlId------" + sqlId);
// System.out.println(sqlId);
// if(sqlId.equals("com.skua.modules.system.mapper.SysDepartMapper.queryUserDeparts")) {
// System.out.println("sss");
// }
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
Object parameter = invocation.getArgs()[1];
log.debug("------sqlCommandType------" + sqlCommandType);
if (parameter == null) {
return invocation.proceed();
}
if (SqlCommandType.INSERT == sqlCommandType) {
Field[] fields = ConvertUtils.getAllFields(parameter);
for (Field field : fields) {
log.debug("------field.name------" + field.getName());
try {
// 关于使用Quzrtz 开启线程任务
// 获取登录用户信息
LoginUser sysUser = null;
try{
sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
}catch (Exception e){
sysUser = null;
}
if ("createBy".equals(field.getName())) {
field.setAccessible(true);
Object local_createBy = field.get(parameter);
field.setAccessible(false);
if (local_createBy == null || local_createBy.equals("")) {
String createBy = "zhsw";
if (sysUser != null) {
// 登录账号
createBy = sysUser.getUsername();
}
if (ConvertUtils.isNotEmpty(createBy)) {
field.setAccessible(true);
field.set(parameter, createBy);
field.setAccessible(false);
}
}
}
// 注入创建时间
if ("createTime".equals(field.getName())) {
field.setAccessible(true);
Object local_createDate = field.get(parameter);
field.setAccessible(false);
if (local_createDate == null || local_createDate.equals("")) {
field.setAccessible(true);
field.set(parameter, new Date());
field.setAccessible(false);
}
}
//注入部门编码
if ("factoryId".equals(field.getName())) {
field.setAccessible(true);
Object local_sysOrgCode = field.get(parameter);
field.setAccessible(false);
if (local_sysOrgCode == null || local_sysOrgCode.equals("")) {
String sysOrgCode = "";
// 获取登录用户信息
if (sysUser != null) {
// 登录账号
sysOrgCode = sysUser.getOrgCode();
}
if (ConvertUtils.isNotEmpty(sysOrgCode)) {
field.setAccessible(true);
field.set(parameter, sysOrgCode);
field.setAccessible(false);
}
}
}
//注入部门编码
if ("createDept".equals(field.getName())) {
field.setAccessible(true);
Object local_sysOrgCode = field.get(parameter);
field.setAccessible(false);
if (local_sysOrgCode == null || local_sysOrgCode.equals("")) {
String sysOrgCode = "";
// 获取登录用户信息
if (sysUser != null) {
// 登录账号
sysOrgCode = sysUser.getOrgCode();
}
if (ConvertUtils.isNotEmpty(sysOrgCode)) {
field.setAccessible(true);
field.set(parameter, sysOrgCode);
field.setAccessible(false);
}
}
}
} catch (Exception e) {
}
}
}
if (SqlCommandType.UPDATE == sqlCommandType) {
Field[] fields = null;
if (parameter instanceof ParamMap) {
ParamMap<?> p = (ParamMap<?>) parameter;
//update-begin-author:scott date:20190729 for:批量更新报错issues/IZA3Q--
if (p.containsKey("et")) {
parameter = p.get("et");
} else {
parameter = p.get("param1");
}
//update-end-author:scott date:20190729 for:批量更新报错issues/IZA3Q-
fields = ConvertUtils.getAllFields(parameter);
} else {
fields = ConvertUtils.getAllFields(parameter);
}
for (Field field : fields) {
log.debug("------field.name------" + field.getName());
try {
if ("updateBy".equals(field.getName())) {
field.setAccessible(true);
Object local_updateBy = field.get(parameter);
field.setAccessible(false);
if (local_updateBy == null || local_updateBy.equals("")) {
String updateBy = "zhsw";
// 获取登录用户信息
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
if (sysUser != null) {
// 登录账号
updateBy = sysUser.getUsername();
}
if (ConvertUtils.isNotEmpty(updateBy)) {
field.setAccessible(true);
field.set(parameter, updateBy);
field.setAccessible(false);
}
}
}
if ("updateTime".equals(field.getName())) {
field.setAccessible(true);
Object local_updateDate = field.get(parameter);
field.setAccessible(false);
if (local_updateDate == null || local_updateDate.equals("")) {
field.setAccessible(true);
field.set(parameter, new Date());
field.setAccessible(false);
}
}
} catch (Exception e) {
}
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
}
}