<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          拼多多二面:Mybatis是如何執(zhí)行一條SQL命令的?

          共 5307字,需瀏覽 11分鐘

           ·

          2020-08-18 02:34

          程序員的成長之路
          互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享?
          關(guān)注


          閱讀本文大概需要 3 分鐘。

          Mybatis中的Sql命令,在枚舉類SqlCommandType中定義的。

          public enum SqlCommandType { UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;}

          下面,我們以Mapper接口中的一個(gè)方法作為例子,看看Sql命令的執(zhí)行完整流程。
          public interface StudentMapper { List findAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh); }

          參數(shù)RowBounds和ResultSetHandler是可選參數(shù),表示分頁對(duì)象和自定義結(jié)果集處理器,一般不需要。

          一個(gè)完整的Sql命令,其執(zhí)行的完整流程圖如下:

          (Made In Edrawmax)

          對(duì)于上面的流程圖,如果看過前面的文章的話,大部分對(duì)象我們都比較熟悉了。一個(gè)圖,就完整展示了其執(zhí)行流程。

          MapperProxy的功能:

          1. 因?yàn)镸apper接口不能直接實(shí)例化,MapperProxy的作用,就是使用JDK動(dòng)態(tài)代理功能,間接實(shí)例化Mapper的proxy對(duì)象。可參看系列的第二篇。

          2. 緩存MapperMethod對(duì)象。
          private final Map methodCache; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class.equals(method.getDeclaringClass())) { try { return method.invoke(this, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } // 投鞭斷流 final MapperMethod mapperMethod = cachedMapperMethod(method); return mapperMethod.execute(sqlSession, args); }
          // 緩存MapperMethod private MapperMethod cachedMapperMethod(Method method) { MapperMethod mapperMethod = methodCache.get(method); if (mapperMethod == null) { mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()); methodCache.put(method, mapperMethod); } return mapperMethod; }

          MapperMethod的功能:

          1. 解析Mapper接口的方法,并封裝成MapperMethod對(duì)象。

          2. 將Sql命令,正確路由到恰當(dāng)?shù)腟qlSession的方法上。
          public class MapperMethod {
          // 保存了Sql命令的類型和鍵id private final SqlCommand command; // 保存了Mapper接口方法的解析信息 private final MethodSignature method;
          public MapperMethod(Class mapperInterface, Method method, Configuration config) { this.command = new SqlCommand(config, mapperInterface, method); this.method = new MethodSignature(config, method); }
          // 根據(jù)解析結(jié)果,路由到恰當(dāng)?shù)腟qlSession方法上 public Object execute(SqlSession sqlSession, Object[] args) { Object result; if (SqlCommandType.INSERT == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.insert(command.getName(), param)); } else if (SqlCommandType.UPDATE == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.update(command.getName(), param)); } else if (SqlCommandType.DELETE == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.delete(command.getName(), param)); } else if (SqlCommandType.SELECT == command.getType()) { if (method.returnsVoid() && method.hasResultHandler()) { executeWithResultHandler(sqlSession, args); result = null; } else if (method.returnsMany()) { result = executeForMany(sqlSession, args); } else if (method.returnsMap()) { result = executeForMap(sqlSession, args); } else { Object param = method.convertArgsToSqlCommandParam(args); result = sqlSession.selectOne(command.getName(), param); } } else if (SqlCommandType.FLUSH == command.getType()) { result = sqlSession.flushStatements(); } else { throw new BindingException("Unknown execution method for: " + command.getName()); } if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) { throw new BindingException("Mapper method '" + command.getName() + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ")."); } return result; } // ...

          org.apache.ibatis.binding.MapperMethod.SqlCommand。

          public static class SqlCommand { // full id, 通過它可以找到MappedStatement private final String name; private final SqlCommandType type;// ...

          org.apache.ibatis.binding.MapperMethod.MethodSignature。

          public static class MethodSignature { private final boolean returnsMany; private final boolean returnsMap; private final boolean returnsVoid; private final Class returnType; private final String mapKey; private final Integer resultHandlerIndex; private final Integer rowBoundsIndex; private final SortedMap params; private final boolean hasNamedParameters;
          public MethodSignature(Configuration configuration, Method method) { this.returnType = method.getReturnType(); this.returnsVoid = void.class.equals(this.returnType); this.returnsMany = (configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray()); this.mapKey = getMapKey(method); this.returnsMap = (this.mapKey != null); this.hasNamedParameters = hasNamedParams(method); // 分頁參數(shù) this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class); // 自定義ResultHandler this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters)); }

          以上是對(duì)MapperMethod的補(bǔ)充說明。本節(jié)的重點(diǎn),是上面的那個(gè)Sql命令完整執(zhí)行流程圖。

          如果不是使用Mapper接口調(diào)用,而是直接調(diào)用SqlSession的方法,那么,流程圖從SqlSession的地方開始即可,后續(xù)都是一樣的。

          推薦閱讀:

          00后大學(xué)生在家發(fā)射火箭,從發(fā)動(dòng)機(jī)到軟件全部自制,「初中時(shí)受馬斯克鼓舞」

          徹底理解瀏覽器的Http緩存機(jī)制

          5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「2048」,即可免費(fèi)獲取!!

          微信掃描二維碼,關(guān)注我的公眾號(hào)

          寫留言

          朕已閱?

          瀏覽 33
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  自拍三级片青青草视频 | www.污污污在线观看 | 俺也来也去精品视频 | 69三级片| 久久精品国产亚洲AV人妖 |