Mybatis自定义插件Interceptor

本文详细介绍了如何在Mybatis中使用Interceptor实现自定义插件,重点讲解了如何拦截`Executor`的`query`方法,实现实时添加SQL条件。通过实例说明,插件可用于简化开发并监控查询行为。
摘要由CSDN通过智能技术生成

Mybatis自定义插件-Interceptor

MyBatis 允许你在映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 发行包中的源代码。 如果你想做的不仅仅是监控方法的调用,那么你最好相当了解要重写的方法的行为。 因为在试图修改或重写已有方法的行为时,很可能会破坏 MyBatis 的核心模块。 这些都是更底层的类和方法,所以使用插件的时候要特别当心。

通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定想要拦截的方法签名即.

@Intercepts(value = {@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor {
    //    @Autowired
    private Logger log = new LoggerStudoImpl();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 该方法写入自己的逻辑
        Object[] queryArgs = invocation.getArgs();
        MappedStatement ms = (MappedStatement) queryArgs[0];
        BoundSql boundSql = ms.getBoundSql(queryArgs[1]);
        String sql = boundSql.getSql();
        String SQL = new StringBuilder(sql).append(" ").append("and deleted = '0'").toString();
        StaticSqlSource rawSqlSource = new StaticSqlSource(ms.getConfiguration(), SQL, boundSql.getParameterMappings());

        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), rawSqlSource, 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());
        MappedStatement statement = builder.build();
        queryArgs[0] = statement;
        log.error(SQL);
        return invocation.proceed();

    }

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

    @Override
    public void setProperties(Properties properties) {

    }


}

然后将自定义插件添加到Mybatis配置文件中,该插件就会生效。

   <plugins>
        <plugin interceptor="com.example.shiro.config.MyInterceptor"></plugin>
    </plugins>

自定义插件拦截的是Excutor中的query方法,也就是说只要是Mybatis执行查询,那么该插件就会拦截查询的SQL语句,将查询的SQL添加上and deleted = ‘0’,这样每次查询就无需加上deleted='0’了。

比如一些分页插件就是拦截的ReultSetHandle进而将查询的结果进行分页处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值