Mybatis拦截提取SQL文

该文章介绍了一个Mybatis的拦截器实现,主要功能是对Executor类的update方法进行拦截,当执行SQL更新时,检查参数并截断超过1000个字符的字段值,同时记录操作日志,包括用户信息、SQL内容和操作时间。如果SQL不涉及tbl_sys_operation_log表,则会创建一个新的操作日志条目。
摘要由CSDN通过智能技术生成
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), })
public class MybatisInterceptor implements Interceptor {
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		try {
			MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
			Object parameter = null;
			if (invocation.getArgs().length > 1) {
				parameter = invocation.getArgs()[1];
			}
			LoginUser user = LoginUser.getLoginUser();
			BoundSql boundSql = mappedStatement.getBoundSql(parameter);
			String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
			List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
			if (parameterMappings != null && parameterMappings.size() > 0) {
				Configuration configuration = mappedStatement.getConfiguration();
				Object parameterObject = boundSql.getParameterObject();
				MetaObject metaObject = configuration.newMetaObject(parameterObject);
				for (ParameterMapping parameterMapping : parameterMappings) {
					String propertyName = parameterMapping.getProperty();
					if (metaObject.hasGetter(propertyName)) {
						Object obj = metaObject.getValue(propertyName);
						String columValue = "";
						if (obj != null && obj.toString().length() > 1000) {
							columValue = obj.toString().substring(0, 999);
						} else if (obj != null && obj.toString().length() < 1000) {
							columValue = obj.toString();
						}
						sql = sql.replaceFirst("\\?", columValue);
					} else if (boundSql.hasAdditionalParameter(propertyName)) {
						Object obj = boundSql.getAdditionalParameter(propertyName);
						String columValue = "";
						if (obj != null && obj.toString().length() > 1000) {
							columValue = obj.toString().substring(0, 999);
						} else if (obj != null && obj.toString().length() < 1000) {
							columValue = obj.toString();
						}
						sql = sql.replaceFirst("\\?", columValue);
					}
				}
			}
			log.info("SQL: {} , user: {}, date : {}", sql, null != user ? user.getName() : "", new Date());
			if (!sql.contains("tbl_sys_operation_log")) {
				DboSysOperationLog log =  new DboSysOperationLog();
				if (user != null) {
					log.setUserId(user.getId());
				} else {
					log.setUserId(8505);
				}
				log.setSqlText(sql);
				log.setOperateTime(new Date());
				log.setModuleName("maintenance");
				DboSysOperationLogService.insert(log);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}

}

使用拦截器进行SQL的拦截保存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值