使用mybatis插件拦截器机制在sql的插入及更新时设置创建人和更新人

橙三吉。 提交于 2020-08-06 03:56:37

 

参考:
https://www.jianshu.com/p/0a72bb1f6a21
https://www.extlight.com/2018/06/13/Mybatis-%E6%8F%92%E4%BB%B6%E5%AE%9E%E7%8E%B0%E5%8A%A8%E6%80%81%E8%AE%BE%E7%BD%AE%E5%8F%82%E6%95%B0/

@Component
@Slf4j

@Intercepts({@Signature(type = Executor.class, method = 'update', args = {MapperdStatement.class, Object.class} ) } )

public class CreateUserInterceptor implements Interceptor {

public Object intercept(Invovation invocation) throws Throwable {

MapperdStatement ms = Invocation.getArgs()[0];

Object parameter = invocation.getArgs()[1];

SqlCommandType sqlCommandType = ms.getSqlCommandType();

Field field = null;

if(SqlCommandType.INSERT.equals(sqlCommandType)){

    field = this.getField(parameter.getClass(),"CREATEUSER");

}esle if(SqlCommandType.UPDATE.equals(sqlCommandType)){

    field = this.getField(parameter.getClass(),"MODIFYUSER");

}

if(filed != null){

    ReflectionUtils.setField(field,parameter,SessionUtils.getSession.getUserName());

}

}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!