mybatis plugin编写

本文介绍了一种基于MyBatis框架的插件开发方法,通过实现Interceptor接口并利用反射技术,可以在执行SQL前进行预处理操作,适用于SQL拦截、日志记录等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尝试使用了一下mybati的插件功能

写了一个基础类备以后使用

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.RowBounds;

@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class })
})
public abstract class AbstractMyBatisPlugin implements Interceptor {

private static final ObjectFactory OBJECT_FACTORY = new DefaultObjectFactory();
private static final ObjectWrapperFactory OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();

private Properties properties;

public abstract void preIntercept(MetaObject metaStatementHandler, Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql, RowBounds rowBounds);

@Override
public final Object intercept(Invocation invocation) throws Throwable {

StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);

// 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环可以分离出最原始的的目标类)
while (metaStatementHandler.hasGetter("h")) {
Object object = metaStatementHandler.getValue("h");
metaStatementHandler = MetaObject.forObject(object, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);
}
// 分离最后一个代理对象的目标类
while (metaStatementHandler.hasGetter("target")) {
Object object = metaStatementHandler.getValue("target");
metaStatementHandler = MetaObject.forObject(object, OBJECT_FACTORY, OBJECT_WRAPPER_FACTORY);
}

Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");

preIntercept(metaStatementHandler, configuration, mappedStatement, boundSql, rowBounds);

return invocation.proceed();
}

@Override
public final Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
} else {
return target;
}
}

public Properties getProperties() {
return properties;
}

@Override
public void setProperties(Properties properties) {
this.properties = properties;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值