| | |
| | | package com.ruoyi.web.controller.interceptor; |
| | | |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import java.lang.reflect.Field; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Properties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.executor.Executor; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlCommandType; |
| | | import org.apache.ibatis.plugin.*; |
| | | 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.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) }) |
| | | @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, |
| | | Object.class})}) |
| | | public class MybatisInterceptor implements Interceptor { |
| | | |
| | | @Autowired |
| | |
| | | @Override |
| | | public Object intercept(Invocation invocation) throws Throwable { |
| | | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; |
| | | log.debug("{}:"+mappedStatement); |
| | | log.debug("{}:" + mappedStatement); |
| | | log.debug("------sqlId------" + mappedStatement.getId()); |
| | | if("com.ruoyi.system.mapper.SysLogininforMapper.insertLogininfor".equals(mappedStatement.getId())){ |
| | | if ("com.ruoyi.system.mapper.SysLogininforMapper.insertLogininfor".equals( |
| | | mappedStatement.getId())) { |
| | | return invocation.proceed(); |
| | | } |
| | | // sql类型:insert、update、select、delete |
| | | SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); |
| | | Object parameter = invocation.getArgs()[1]; |
| | | log.debug("------sqlCommandType------" + sqlCommandType); |
| | | |
| | | |
| | | if (parameter == null) { |
| | | return invocation.proceed(); |
| | | } |
| | | |
| | | |
| | | // 当sql为新增或更新类型时,自动填充操作人相关信息 |
| | | if (SqlCommandType.INSERT == sqlCommandType) { |
| | | |
| | |
| | | for (Field field : fields) { |
| | | try { |
| | | // 注入创建人 |
| | | if ("createBy".equals(field.getName())) { |
| | | /* if ("createBy".equals(field.getName())) { |
| | | // 获取当前登录用户信息 |
| | | if(Objects.nonNull(tokenService.getLoginUser())){ |
| | | if (Objects.nonNull(tokenService.getLoginUser())) { |
| | | String userName = tokenService.getLoginUser().getUser().getUserName(); |
| | | field.setAccessible(true); |
| | | field.set(parameter, userName); |
| | | field.setAccessible(false); |
| | | } |
| | | } |
| | | }*/ |
| | | //注入创建时间 |
| | | if ("createTime".equals(field.getName())) { |
| | | field.setAccessible(true); |
| | | field.set(parameter, LocalDateTime.now()); |
| | | field.set(parameter, new Date()); |
| | | field.setAccessible(false); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("failed to insert data, exception = ", e); |
| | | } |
| | |
| | | try { |
| | | if ("updateBy".equals(field.getName())) { |
| | | // 获取当前登录用户信息 |
| | | if(Objects.nonNull(tokenService.getLoginUser())){ |
| | | if (Objects.nonNull(tokenService.getLoginUser())) { |
| | | String userName = tokenService.getLoginUser().getUser().getUserName(); |
| | | field.setAccessible(true); |
| | | field.set(parameter, userName); |
| | |
| | | } |
| | | if ("updateTime".equals(field.getName())) { |
| | | field.setAccessible(true); |
| | | field.set(parameter, LocalDateTime.now()); |
| | | 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 |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取类的所有属性,包括父类 |
| | | * |
| | |
| | | fieldList.toArray(fields); |
| | | return fields; |
| | | } |
| | | |
| | | |
| | | } |