mybatis通过拦截器动态的修改sql
比如你的orderInfo有个版本号,你需要每次修改的时候版本号增加1,如果手工添加比较麻烦,容易遗漏,可以通过拦截器实现。添加MybatisConfig,注入拦截器。
·
比如你的orderInfo有个版本号,你需要每次修改的时候版本号增加1,如果手工添加比较麻烦,容易遗漏,可以通过拦截器实现。
这里适合通过mybatis根据实体类更新的情况,如果是手工写的update 代码,则可以把
parameter instanceof OrderInfo更换为
sql.contains(" order_info ")
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Properties;
//只拦截update请求
@Intercepts({@Signature(
type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class}
)})
@Slf4j
public class MybatisUpdateInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 获取当前执行的SQL语句
Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement) args[0];
Object parameter = args[1];
// 判断当前执行的SQL语句是否是修改orderInfo表的数据
if (mappedStatement.getSqlCommandType().equals(SqlCommandType.UPDATE) && parameter instanceof OrderInfo) {
//如果是修改order_info表,修改version
PropertyUtils.setProperty(parameter, "version", null);
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String sql = boundSql.getSql();
sql = sql.replace(" WHERE ", ",`version`=(version+1) WHERE ");
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sql, boundSql.getParameterMappings(), parameter);
// 把新的查询放到statement里
MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
args[0] = newMs;
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
// do nothing
}
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
private static class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
@Override
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
}
添加MybatisConfig,注入拦截器
/**
* mybatis配置
*/
@Configuration
public class MybatisConfig {
/**
* 注册拦截器
*/
@Bean
public MybatisUpdateInterceptor mybatisInterceptor() {
MybatisUpdateInterceptor interceptor = new MybatisUpdateInterceptor();
return interceptor;
}
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)