欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor)

首先我先解釋一下標(biāo)題 四大對(duì)象是指:executor, statementHandler,parameterHandler,resultHandler對(duì)象。(為了方便下面的文章說道四大對(duì)象就專指它們)

公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出萬州免費(fèi)做網(wǎng)站回饋大家。

它們都是sqlSession的底層類實(shí)現(xiàn),也是插件能夠攔截的四大對(duì)象。所以這里已經(jīng)觸及了MyBATIS的底層,動(dòng)態(tài)代理,反射隨時(shí)可以看到,如果沒有第一篇作為基礎(chǔ),你將十分難以理解它。了解他們的協(xié)作,是插件編寫的基礎(chǔ)之一,所以這是十分的重要。

Executor在sqlSession中的應(yīng)用

上篇我們談到了一個(gè)問題,一個(gè)mapper被執(zhí)行是通過動(dòng)態(tài)代理來完成的,然后進(jìn)入到了sqlSession的方法中去。這個(gè)并不難理解,但是sqlSession內(nèi)部是怎么運(yùn)行的呢?答案四大對(duì)象的協(xié)作。在SqlSession它還是一個(gè)接口,mybatis內(nèi)部是通過DefaultSqlSession這個(gè)實(shí)現(xiàn)類為我們提供服務(wù)的,它比較長(zhǎng),但是我們不需要全部看到,我們只看到很常用的selectList方法便可以了。

package org.apache.ibatis.session.defaults;
public class DefaultSqlSession implements SqlSession {
 private Configuration configuration;
 private Executor executor;
 private boolean autoCommit;
 private boolean dirty;
.......
@Override
 public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  try {
   MappedStatement ms = configuration.getMappedStatement(statement);
   return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
  } catch (Exception e) {
   throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
  } finally {
   ErrorContext.instance().reset();
  }
 }
......
}

我們可以看到它是通過executor去執(zhí)行方法來完成查詢的。

初認(rèn)Executor

那么我們對(duì)executor就很感興趣,于是我們看看executor是怎么樣的,首先在MyBATIS中有三種executor:

SimpleExecutor -- SIMPLE 就是普通的執(zhí)行器。

ReuseExecutor -執(zhí)行器會(huì)重用預(yù)處理語(yǔ)句(prepared statements)

BatchExecutor --它是批量執(zhí)行器

這些就是mybatis的三種執(zhí)行器。你可以通過配置文件的settings里面的元素defaultExecutorType,配置它,默認(rèn)是采用SimpleExecutor如果你在Spring運(yùn)用它,那么你可以這么配置它:

<bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">   
<constructor-arg index="0" ref="sqlSessionFactory" /> 
<!--更新采用批量的executor --> 
<constructor-arg index="1" value="BATCH"/> 
</bean> 

這樣,它便是一個(gè)批量的執(zhí)行器。mybatis的三個(gè)executor都有一個(gè)共同的父類——BaseExecutor。

Executor初始化

首先我們先了解一下mybatis是怎么樣生成executor的。我們看到生成Executor的地方(org.apache.ibatis.session.Configuration):

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
  executorType = executorType == null ? defaultExecutorType : executorType;
  executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  Executor executor;
  if (ExecutorType.BATCH == executorType) {
   executor = new BatchExecutor(this, transaction);
  } else if (ExecutorType.REUSE == executorType) {
   executor = new ReuseExecutor(this, transaction);
  } else {
   executor = new SimpleExecutor(this, transaction);
  }
  if (cacheEnabled) {
   executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);
  return executor;
 }

這里大部分都很好理解,但是有個(gè)地方就好不好理解,它就是:

executor = (Executor) interceptorChain.pluginAll(executor);

這是一段非常重要的代碼,它是采用責(zé)任鏈模式,來產(chǎn)生代理對(duì)象。我們需要再深入理解它,打開它具體的pluginAll方法:

public Object pluginAll(Object target) {
  for (Interceptor interceptor : interceptors) {
   target = interceptor.plugin(target);
  }
  return target;
 }

我們這里先介紹一下這段代碼:

Interceptor它是mybatis攔截器必須要實(shí)現(xiàn)的接口,換句話說,這個(gè)遍歷就是遍歷mybatis的攔截器。

然后調(diào)用plugin方法,這個(gè)方法是為了生成代理對(duì)象(占位)的。

于是可以想象我們的插件的代理對(duì)象將會(huì)是一層層的嵌套,所以當(dāng)中任何一個(gè)插件(Interceptor)都有機(jī)會(huì)攔截這個(gè)真是的服務(wù)對(duì)象(executor),則便是責(zé)任鏈模式,我們完全可以提供插件(Interceptor),進(jìn)入到代理對(duì)象的invoke方法里面,來改變executor的行為和方法。

但是我要在這里強(qiáng)調(diào),當(dāng)你使用插件的時(shí)候,你將改變mybatis的executor內(nèi)容實(shí)現(xiàn),你必須慎重的使用它。

如果要我們自己編寫動(dòng)態(tài)的代理,那么工作量可不小,好在mybatis為我們提供了Plugin.java類來完成我們所需要的功能。

public class Plugin implements InvocationHandler {
 private Object target;
 private Interceptor interceptor;
 private Map<Class<?>, Set<Method>> signatureMap;
 private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
  this.target = target;
  this.interceptor = interceptor;
  this.signatureMap = signatureMap;
 }
 public static Object wrap(Object target, Interceptor interceptor) {
  Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
  Class<?> type = target.getClass();
  Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
  if (interfaces.length > 0) {
   return Proxy.newProxyInstance(
     type.getClassLoader(),
     interfaces,
     new Plugin(target, interceptor, signatureMap));
  }
  return target;
 }
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  try {
   Set<Method> methods = signatureMap.get(method.getDeclaringClass());
   if (methods != null && methods.contains(method)) {
    return interceptor.intercept(new Invocation(target, method, args));
   }
   return method.invoke(target, args);
  } catch (Exception e) {
   throw ExceptionUtil.unwrapThrowable(e);
  }
 }
 ......
}

這里有一個(gè)wrap方法:它會(huì)為我們生成代理對(duì)象。一旦我們的插件和它綁定,那么我們可以想到就會(huì)進(jìn)入invoke方法里面。

invoke方法:很簡(jiǎn)單,它運(yùn)行首先通過class和method的過濾,看看是否需要攔截這個(gè)方法,如果被攔截,那么它就運(yùn)行interceptor的intercept方法。所以當(dāng)我們配置了簽名,就能夠攔截我們的方法。

我們先討論那么多,我們知道后面講插件的時(shí)候我們還會(huì)提及它,這里我們知道它會(huì)根據(jù)插件的個(gè)數(shù)生成一層層的代理對(duì)象就可以了。

executor的執(zhí)行

executor的執(zhí)行是依賴于Statement對(duì)象來操作的,讓我們以SimpleExecutor的doQuery方法為例子:

public class SimpleExecutor extends BaseExecutor {
......
 @Override
 public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
   Configuration configuration = ms.getConfiguration();
   StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
   stmt = prepareStatement(handler, ms.getStatementLog());
   return handler.<E>query(stmt, resultHandler);
  } finally {
   closeStatement(stmt);
  }
 }
 ......
 private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
  Statement stmt;
  Connection connection = getConnection(statementLog);
  stmt = handler.prepare(connection);
  handler.parameterize(stmt);
  return stmt;
 }
}

很顯然這里調(diào)度的是一個(gè)查詢方法

首先它先生成StatementHandler對(duì)象。

通過prepareStatement方法調(diào)用prepare方法初始化參數(shù)。

然后使用parameterize方法設(shè)置參數(shù)到運(yùn)行環(huán)境。

然后便通過handler.<E>query(stmt, resultHandler);方法來完成結(jié)果組裝。

于是我們的焦點(diǎn)就集中在了StatementHandler對(duì)象上,下章我們將談及它。

總結(jié)

以上所述是小編給大家介紹的Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

文章名稱:Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor)
文章鏈接:http://chinadenli.net/article22/ppjhjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站維護(hù)、定制開發(fā)、網(wǎng)站收錄、商城網(wǎng)站、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)