MyBatis四大对象及插件原理

1.四大对象:

ParameterHandler:处理SQL的参数对象  

StatementHandler:数据库的处理对象,用于执行SQL语句      

Executor:MyBatis的执行器,用于执行增删改查操作          

ResultSetHandler:处理SQL的返回结果集

追踪四大对象的源码:

org.apache.ibatis.executor.parameter.ParameterHandler

从源码可以看到,该接口类里的setParameters方法

org.apache.ibatis.executor.statement.StatementHandler;

BaseStatementHandler:StatementHandler的实现类

Executor:执行sql,提交事务,处理事务。
public interface Executor {

  ResultHandler NO_RESULT_HANDLER = null;

  int update(MappedStatement ms, Object parameter) throws SQLException;

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;

  <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException;

  List<BatchResult> flushStatements() throws SQLException;
  //提交事务
  void commit(boolean required) throws SQLException;
  //回滚事务
  void rollback(boolean required) throws SQLException;
  //myBatis缓存
  CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
  //开启缓存
  boolean isCached(MappedStatement ms, CacheKey key);
  //清除缓存
  void clearLocalCache();

  void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);

  Transaction getTransaction();

  void close(boolean forceRollback);

  boolean isClosed();

  void setExecutorWrapper(Executor executor);

}
ResultSetHandler:处理结果集
public interface ResultSetHandler {

  <E> List<E> handleResultSets(Statement stmt) throws SQLException;

  <E> Cursor<E> handleCursorResultSets(Statement stmt) throws SQLException;

  void handleOutputParameters(CallableStatement cs) throws SQLException;

}
DefaultResultSetHandler实现了ResultSetHandler 接口

插件原理:MyBatis的这四个对象都是可以拦截的,通过动态代理的方式拦截其中的对象,改变底层的设计代码。框架里提供了Intecptor

public interface Interceptor {

  Object intercept(Invocation invocation) throws Throwable;

  Object plugin(Object target);

  void setProperties(Properties properties);

}
/**
*实现拦截器接口编些插件
*@Intercepts 拦截器注解
*@Signature
*type 拦截的对象
*method 拦截的方法
*args参数
*/
@Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class})})
public class TestInterceptor  implements Interceptor{
/*
*实现拦截方法
*@param Invocation
*@return Object
*/
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        String id = mappedStatement.getId();
        mappedStatement.getParameterMap();
        ...
       //进入责任链下一层
        return invocation.proceed();
    }
    /**
    *返回代理对象
    */ 
    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor){
            return Plugin.wrap(target,this);
        }
        return target;
    }
    /**
    *设置插件参数
   */ 
    @Override
    public void setProperties(Properties properties) {

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值