当前位置 : 主页 > 编程语言 > java >

拦截增删改sql,并在事务提交中做后续操作

来源:互联网 收集:自由互联 发布时间:2022-09-02
import com . alibaba . fastjson . JSON ; import org . apache . ibatis . executor . statement . StatementHandler ; import org . apache . ibatis . mapping . BoundSql ; import org . apache . ibatis . mapping . ParameterMapping ; import org . a


import com.alibaba.fastjson.JSON;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

@Intercepts({@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
@Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
@Component
@Configuration
public class SqlStatementInterceptor implements Interceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(SqlStatementInterceptor.class);

@Override
public Object intercept(Invocation invocation) {
// 开始时间
try {
Object target = invocation.getTarget();
StatementHandler statementHandler = (StatementHandler)target;

BoundSql boundSql = statementHandler.getBoundSql();

//boundSql.getSql();
String sql = showSql(boundSql);
System.out.println(sql);
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappingList = boundSql.getParameterMappings();
// System.out.println(sql);

String updatestr= JSON.toJSONString(boundSql);
// 注册事务同步处理
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
// 事务提交完毕时,触发:funcB
System.out.println("事务提交后!");
System.out.println(sql);
}
});
return invocation.proceed();
}catch (Exception e) {
LOGGER.error("执行失败!", e);
e.printStackTrace();
return null;
}
}

@Override
public Object plugin(Object arg0) {
return Plugin.wrap(arg0, new SqlStatementInterceptor());
}

@Override
public void setProperties(Properties arg0) {
}


private String getParameterValue(Object obj) {
//to_timestamp(to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')
String value = null;
if (obj instanceof String) {
value = "'" + obj.toString() + "'";
} else if (obj instanceof java.sql.Timestamp) {
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
value = "to_timestamp(to_char(" + formatter.format(obj) + ",'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')";
}
else if (obj instanceof Date) {
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
value = "'" + formatter.format(obj) + "'";
// System.out.println(value);
} else {
if (obj != null) {
value = obj.toString();
} else {
value = "";
}

}
return value;
}

public String showSql(BoundSql boundSql) {
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
if (parameterMappings.size() > 0 && parameterObject != null) {
//TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
// if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
// sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
//
// } else {
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
}
}
// }
}
return sql;
}
}


网友评论