一、Spring MethodInterceptor拦截器配置
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.oncloudit.shared.util.StringUtil;
public class SQLConvertorInterceptor implements MethodInterceptor {
public String spliceUpdateSQL(String sql) {
if (!StringUtil.isNotEmpty(sql)) return null;
//1.检查该sql操作的表是否是需要同步的表
//2.检查该sql是否是update操作
if (sql.contains("UPDATE")) {
int index = sql.indexOf("SET");
String a = sql.substring(0, index);
String c = sql.substring(index + 4);
sql = a + "SET UPDATE_REASON = 10, " + c;
}
return sql;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args = invocation.getArguments();
for (int i = 0; i < args.length; i++) {
Object o = args[i];
if (o == null || !(o instanceof String)) continue;
args[i] = spliceUpdateSQL(o.toString());
}
return invocation.proceed();
}
}
二、Spring AOP配置
<bean id="sqlConvertorInterceptor" class="SQLConvertorInterceptor"/> <aop:config> <aop:pointcut id="pointCut" expression="execution(* *.baseOp(..))"/> <aop:advisor advice-ref="sqlConvertorInterceptor" pointcut-ref="pointCut"/> </aop:config>
三、示例
BaseDao baseDao = appContext.getBean("baseDao", BaseDao.class);
baseDao.baseOp("UPDATE t_order_log SET bid = 543, op_time = SYSDATE(), OPERATOR_ID = 500 WHERE bid = 543", null);
使用此配置后该SQL被修改如下, 然后再执行:
UPDATE t_order_log SET UPDATE_REASON = 10, bid = 543, op_time = SYSDATE(), OPERATOR_ID = 500 WHERE bid = 543