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.BoundSql; 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.Configuration; import org.apache.shiro.SecurityUtils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; /** * mybatis攔截器,自動注入創建人、創建時間、修改人、修改時間 */ @Slf4j @Component @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) public class MybatisInterceptor implements Interceptor { @Autowired private ILogDelService logDelService; @Autowired private ILogUpService logUpService; @Override public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; String sqlId = mappedStatement.getId(); log.debug("------sqlId------" + sqlId); SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); Object parameter = invocation.getArgs()[1]; log.debug("------sqlCommandType------" + sqlCommandType); BoundSql boundSql = mappedStatement.getBoundSql(parameter); // BoundSql就是封裝myBatis最終產生的sql類 Configuration configuration = mappedStatement.getConfiguration(); // 獲取節點的配置 String sql = SplicSqlUtils.getSql(configuration, boundSql, sqlId); // 獲取到最終的sql語句 if (parameter == null) { return invocation.proceed(); } if (SqlCommandType.INSERT == sqlCommandType) { LoginUser sysUser = this.getLoginUser(); Field[] fields = oConvertUtils.getAllFields(parameter); for (Field field : fields) { log.debug("------field.name------" + field.getName()); try { if ("createBy".equals(field.getName())) { field.setAccessible(true); Object local_createBy = field.get(parameter); field.setAccessible(false); if (local_createBy == null || local_createBy.equals("")) { if (sysUser != null) { // 登錄人賬號 field.setAccessible(true); field.set(parameter, sysUser.getUsername()); 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 ("sysOrgCode".equals(field.getName())) { field.setAccessible(true); Object local_sysOrgCode = field.get(parameter); field.setAccessible(false); if (local_sysOrgCode == null || local_sysOrgCode.equals("")) { // 獲取登錄用戶信息 if (sysUser != null) { field.setAccessible(true); field.set(parameter, sysUser.getOrgCode()); field.setAccessible(false); } } } } catch (Exception e) { } } } if (SqlCommandType.UPDATE == sqlCommandType) { // 添加修改記錄 LogUp logUp = new LogUp(); logUp.setSqlStr(sql); logUp.setCreateTime(DateUtils.getDate()); logUp.setIsUsed(0); logUpService.save(logUp); LoginUser sysUser = this.getLoginUser(); Field[] fields; 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- //update-begin-author:scott date:20190729 for:更新指定字段時報錯 issues/#516- if (parameter == null) { return invocation.proceed(); } //update-end-author:scott date:20190729 for:更新指定字段時報錯 issues/#516- fields = oConvertUtils.getAllFields(parameter); } else { fields = oConvertUtils.getAllFields(parameter); } for (Field field : fields) { log.debug("------field.name------" + field.getName()); try { if ("updateBy".equals(field.getName())) { //獲取登錄用戶信息 if (sysUser != null) { // 登錄賬號 field.setAccessible(true); field.set(parameter, sysUser.getUsername()); field.setAccessible(false); } } if ("updateTime".equals(field.getName())) { field.setAccessible(true); field.set(parameter, new Date()); field.setAccessible(false); } } catch (Exception e) { e.printStackTrace(); } } } if (SqlCommandType.DELETE == sqlCommandType) { // 添加刪除記錄 LogDel logDel = new LogDel(); logDel.setSqlStr(sql); logDel.setCreateTime(DateUtils.getDate()); logDel.setIsUsed(0); logDelService.save(logDel); } 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 } //update-begin--Author:scott Date:20191213 for:關於使用Quzrtz 開啟線程任務, #465 private LoginUser getLoginUser() { LoginUser sysUser; try { sysUser = SecurityUtils.getSubject().getPrincipal() != null ? (LoginUser) SecurityUtils.getSubject().getPrincipal() : null; } catch (Exception e) { //e.printStackTrace(); sysUser = null; } return sysUser; } //update-end--Author:scott Date:20191213 for:關於使用Quzrtz 開啟線程任務, #465 }