Mybatis:Mapper 接口編程原理分析(五)

在上一篇文章Mybatis:Mapper接口編程原理分析(四)中缕碎,我們已經(jīng)知道最終會進(jìn)入 MapperMethod 類的 execute 方法進(jìn)行正在的 CRUD 處理患亿,現(xiàn)在才是真正的核心野芒,前面都是為了現(xiàn)在做準(zhǔn)備。因此嘱朽,我們著重分析栏渺。

  • MapperMethod
//這個類是整個代理機(jī)制的核心類,對Sqlsession當(dāng)中的操作進(jìn)行了封裝  
public class MapperMethod {  
  //一個內(nèi)部封 封裝了SQL標(biāo)簽的類型 insert update delete select  
  private final SqlCommand command;  
  //一個內(nèi)部類 封裝了方法的參數(shù)信息 返回類型信息等  
  private final MethodSignature method;  
  
  //構(gòu)造參數(shù)  
  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {  
    this.command = new SqlCommand(config, mapperInterface, method);  
    this.method = new MethodSignature(config, method);  
  }  
  
  //這個方法是對SqlSession的包裝調(diào)用  
  public Object execute(SqlSession sqlSession, Object[] args) {  
    //定義返回結(jié)果  
    Object result;  
    //如果是INSERT操作  
    if (SqlCommandType.INSERT == command.getType()) {  
      //處理參數(shù)  
      Object param = method.convertArgsToSqlCommandParam(args);  
      //調(diào)用sqlSession的insert方法  
      result = rowCountResult(sqlSession.insert(command.getName(), param));  
      //如果是UPDATE操作 同上  
    } else if (SqlCommandType.UPDATE == command.getType()) {  
      Object param = method.convertArgsToSqlCommandParam(args);  
      result = rowCountResult(sqlSession.update(command.getName(), param));  
      //如果是DELETE操作 同上  
    } else if (SqlCommandType.DELETE == command.getType()) {  
      Object param = method.convertArgsToSqlCommandParam(args);  
      result = rowCountResult(sqlSession.delete(command.getName(), param));  
      //如果是SELECT操作 那么情況會多一些 但是也都和sqlSession的查詢方法一一對應(yīng)  
    } else if (SqlCommandType.SELECT == command.getType()) {  
       //如果返回void 并且參數(shù)有resultHandler  
      //則調(diào)用 void select(String statement, Object parameter, ResultHandler handler);方法  
      if (method.returnsVoid() && method.hasResultHandler()) {  
        executeWithResultHandler(sqlSession, args);  
        result = null;  
      //如果返回多行結(jié)果這調(diào)用 <E> List<E> selectList(String statement, Object parameter);  
      //executeForMany這個方法調(diào)用的  
      } else if (method.returnsMany()) {  
        result = executeForMany(sqlSession, args);  
      //如果返回類型是MAP 則調(diào)用executeForMap方法  
      } 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 {  
      //如果全都不匹配 說明mapper中定義的方法不對  
      throw new BindingException("Unknown execution method for: " + command.getName());  
    }  
    //如果返回值為空 并且方法返回值類型是基礎(chǔ)類型 并且不是VOID 則拋出異常  
    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;  
  }  
  
  private Object rowCountResult(int rowCount) {  
    final Object result;  
    if (method.returnsVoid()) {  
      result = null;  
    } else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {  
      result = Integer.valueOf(rowCount);  
    } else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {  
      result = Long.valueOf(rowCount);  
    } else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {  
      result = Boolean.valueOf(rowCount > 0);  
    } else {  
      throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());  
    }  
    return result;  
  }  
  
  private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {  
    MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());  
    if (void.class.equals(ms.getResultMaps().get(0).getType())) {  
      throw new BindingException("method " + command.getName()   
          + " needs either a @ResultMap annotation, a @ResultType annotation,"   
          + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");  
    }  
    Object param = method.convertArgsToSqlCommandParam(args);  
    if (method.hasRowBounds()) {  
      RowBounds rowBounds = method.extractRowBounds(args);  
      sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));  
    } else {  
      sqlSession.select(command.getName(), param, method.extractResultHandler(args));  
    }  
  }  
  //返回多行結(jié)果 調(diào)用sqlSession.selectList方法  
  private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {  
    List<E> result;  
    Object param = method.convertArgsToSqlCommandParam(args);  
    //如果參數(shù)含有rowBounds則調(diào)用分頁的查詢  
    if (method.hasRowBounds()) {  
      RowBounds rowBounds = method.extractRowBounds(args);  
      result = sqlSession.<E>selectList(command.getName(), param, rowBounds);  
    } else {  
    //沒有分頁則調(diào)用普通查詢  
      result = sqlSession.<E>selectList(command.getName(), param);  
    }  
    // issue #510 Collections & arrays support  
    if (!method.getReturnType().isAssignableFrom(result.getClass())) {  
      if (method.getReturnType().isArray()) {  
        return convertToArray(result);  
      } else {  
        return convertToDeclaredCollection(sqlSession.getConfiguration(), result);  
      }  
    }  
    return result;  
  }  
  
  private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {  
    Object collection = config.getObjectFactory().create(method.getReturnType());  
    MetaObject metaObject = config.newMetaObject(collection);  
    metaObject.addAll(list);  
    return collection;  
  }  
  
  @SuppressWarnings("unchecked")  
  private <E> E[] convertToArray(List<E> list) {  
    E[] array = (E[]) Array.newInstance(method.getReturnType().getComponentType(), list.size());  
    array = list.toArray(array);  
    return array;  
  }  
  
  private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {  
    Map<K, V> result;  
    Object param = method.convertArgsToSqlCommandParam(args);  
    if (method.hasRowBounds()) {  
      RowBounds rowBounds = method.extractRowBounds(args);  
      result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey(), rowBounds);  
    } else {  
      result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey());  
    }  
    return result;  
  }  
  
  public static class ParamMap<V> extends HashMap<String, V> {  
  
    private static final long serialVersionUID = -2212268410512043556L;  
  
    @Override  
    public V get(Object key) {  
      if (!super.containsKey(key)) {  
        throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + keySet());  
      }  
      return super.get(key);  
    }  
  
  }  
  //一個內(nèi)部類 封裝了具體執(zhí)行的動作  
  public static class SqlCommand {  
  
    //xml標(biāo)簽的id  
    private final String name;  
    //insert update delete select的具體類型  
    private final SqlCommandType type;  
  
    public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {  
      //拿到全名 接口名加上方法名  
      String statementName = mapperInterface.getName() + "." + method.getName();  
      MappedStatement ms = null;  
      //獲取MappedStatement對象 這個對象封裝了XML當(dāng)中一個標(biāo)簽的所有信息 比如下面  
      //<select id="selectBlog" resultType="Blog">  
      //select * from Blog where id = #{id}  
      //</select>  
      if (configuration.hasStatement(statementName)) {  
        ms = configuration.getMappedStatement(statementName);  
      } else if (!mapperInterface.equals(method.getDeclaringClass())) { // issue #35  
        String parentStatementName = method.getDeclaringClass().getName() + "." + method.getName();  
        if (configuration.hasStatement(parentStatementName)) {  
          ms = configuration.getMappedStatement(parentStatementName);  
        }  
      }  
      //為空拋出異常  
      if (ms == null) {  
        if(method.getAnnotation(Flush.class) != null){  
          name = null;  
          type = SqlCommandType.FLUSH;  
        } else {  
          throw new BindingException("Invalid bound statement (not found): " + statementName);  
        }  
      } else {  
        name = ms.getId();  
        type = ms.getSqlCommandType();  
        //判斷SQL標(biāo)簽類型 未知就拋異常  
        if (type == SqlCommandType.UNKNOWN) {  
          throw new BindingException("Unknown execution method for: " + name);  
        }  
      }  
    }  
  
    public String getName() {  
      return name;  
    }  
  
    public SqlCommandType getType() {  
      return type;  
    }  
  }  
  //內(nèi)部類 封裝了接口當(dāng)中方法的 參數(shù)類型 返回值類型 等信息  
  public static class MethodSignature {  
    //是否返回多調(diào)結(jié)果  
    private final boolean returnsMany;  
    //返回值是否是MAP  
    private final boolean returnsMap;  
    //返回值是否是VOID  
    private final boolean returnsVoid;  
    //返回值類型  
    private final Class<?> returnType;  
    //mapKey  
    private final String mapKey;  
    //resultHandler類型參數(shù)的位置  
    private final Integer resultHandlerIndex;  
    //rowBound類型參數(shù)的位置  
    private final Integer rowBoundsIndex;  
    //用來存放參數(shù)信息  
    private final SortedMap<Integer, String> params;  
    //是否存在命名參數(shù)  
    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);  
      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);  
      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);  
      this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));  
    }  
  
    public Object convertArgsToSqlCommandParam(Object[] args) {  
      final int paramCount = params.size();  
      if (args == null || paramCount == 0) {  
        return null;  
      } else if (!hasNamedParameters && paramCount == 1) {  
        return args[params.keySet().iterator().next().intValue()];  
      } else {  
        final Map<String, Object> param = new ParamMap<Object>();  
        int i = 0;  
        for (Map.Entry<Integer, String> entry : params.entrySet()) {  
          param.put(entry.getValue(), args[entry.getKey().intValue()]);  
          // issue #71, add param names as param1, param2...but ensure backward compatibility  
          final String genericParamName = "param" + String.valueOf(i + 1);  
          if (!param.containsKey(genericParamName)) {  
            param.put(genericParamName, args[entry.getKey()]);  
          }  
          i++;  
        }  
        return param;  
      }  
    }  
  
    public boolean hasRowBounds() {  
      return rowBoundsIndex != null;  
    }  
  
    public RowBounds extractRowBounds(Object[] args) {  
      return hasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null;  
    }  
  
    public boolean hasResultHandler() {  
      return resultHandlerIndex != null;  
    }  
  
    public ResultHandler extractResultHandler(Object[] args) {  
      return hasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null;  
    }  
  
    public String getMapKey() {  
      return mapKey;  
    }  
  
    public Class<?> getReturnType() {  
      return returnType;  
    }  
  
    public boolean returnsMany() {  
      return returnsMany;  
    }  
  
    public boolean returnsMap() {  
      return returnsMap;  
    }  
  
    public boolean returnsVoid() {  
      return returnsVoid;  
    }  
  
    private Integer getUniqueParamIndex(Method method, Class<?> paramType) {  
      Integer index = null;  
      final Class<?>[] argTypes = method.getParameterTypes();  
      for (int i = 0; i < argTypes.length; i++) {  
        if (paramType.isAssignableFrom(argTypes[i])) {  
          if (index == null) {  
            index = i;  
          } else {  
            throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");  
          }  
        }  
      }  
      return index;  
    }  
  
    private String getMapKey(Method method) {  
      String mapKey = null;  
      if (Map.class.isAssignableFrom(method.getReturnType())) {  
        final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);  
        if (mapKeyAnnotation != null) {  
          mapKey = mapKeyAnnotation.value();  
        }  
      }  
      return mapKey;  
    }  
  
    private SortedMap<Integer, String> getParams(Method method, boolean hasNamedParameters) {  
      final SortedMap<Integer, String> params = new TreeMap<Integer, String>();  
      final Class<?>[] argTypes = method.getParameterTypes();  
      for (int i = 0; i < argTypes.length; i++) {  
        if (!RowBounds.class.isAssignableFrom(argTypes[i]) && !ResultHandler.class.isAssignableFrom(argTypes[i])) {  
          String paramName = String.valueOf(params.size());  
          if (hasNamedParameters) {  
            paramName = getParamNameFromAnnotation(method, i, paramName);  
          }  
          params.put(i, paramName);  
        }  
      }  
      return params;  
    }  
  
    private String getParamNameFromAnnotation(Method method, int i, String paramName) {  
      final Object[] paramAnnos = method.getParameterAnnotations()[i];  
      for (Object paramAnno : paramAnnos) {  
        if (paramAnno instanceof Param) {  
          paramName = ((Param) paramAnno).value();  
          break;  
        }  
      }  
      return paramName;  
    }  
  
    private boolean hasNamedParams(Method method) {  
      final Object[][] paramAnnos = method.getParameterAnnotations();  
      for (Object[] paramAnno : paramAnnos) {  
        for (Object aParamAnno : paramAnno) {  
          if (aParamAnno instanceof Param) {  
            return true;  
          }  
        }  
      }  
      return false;  
    }  
  
  }  
  
}  

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市犬辰,隨后出現(xiàn)的幾起案子嗦篱,更是在濱河造成了極大的恐慌,老刑警劉巖幌缝,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灸促,死亡現(xiàn)場離奇詭異,居然都是意外死亡狮腿,警方通過查閱死者的電腦和手機(jī)腿宰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缘厢,“玉大人吃度,你說我怎么就攤上這事√颍” “怎么了椿每?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長英遭。 經(jīng)常有香客問我间护,道長,這世上最難降的妖魔是什么挖诸? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任汁尺,我火速辦了婚禮,結(jié)果婚禮上多律,老公的妹妹穿的比我還像新娘痴突。我一直安慰自己,他們只是感情好狼荞,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布辽装。 她就那樣靜靜地躺著,像睡著了一般相味。 火紅的嫁衣襯著肌膚如雪拾积。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天丰涉,我揣著相機(jī)與錄音拓巧,去河邊找鬼。 笑死昔搂,一個胖子當(dāng)著我的面吹牛玲销,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播摘符,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼贤斜,長吁一口氣:“原來是場噩夢啊……” “哼策吠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起瘩绒,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤猴抹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锁荔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蟀给,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年阳堕,在試婚紗的時候發(fā)現(xiàn)自己被綠了跋理。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡恬总,死狀恐怖前普,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情壹堰,我是刑警寧澤拭卿,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站贱纠,受9級特大地震影響峻厚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谆焊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一惠桃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辖试,春花似錦刽射、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽懈息。三九已至肾档,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辫继,已是汗流浹背怒见。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留姑宽,地道東北人遣耍。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像炮车,于是被迫代替她去往敵國和親舵变。 傳聞我的和親對象是個殘疾皇子酣溃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 1 引言# 本文主要講解JDBC怎么演變到Mybatis的漸變過程,重點(diǎn)講解了為什么要將JDBC封裝成Mybait...
    七寸知架構(gòu)閱讀 76,497評論 36 980
  • 11 MyBatis一級緩存實(shí)現(xiàn)# 11.1 什么是一級緩存纪隙? 為什么使用一級緩存赊豌?## 每當(dāng)我們使用MyBati...
    七寸知架構(gòu)閱讀 10,854評論 12 143
  • 歷法有能力讓人忘記,讓人清算绵咱,讓人感傷碘饼,讓人期待,但其實(shí)都沒有悲伶。時間在這里就像一條本來就很長還在延展的繩子艾恼,每三百...
    空瑾閱讀 176評論 0 0
  • 昨天也沒有什么活動,就是在家待著麸锉,早上倒是起來的時候是十點(diǎn)左右钠绍,迷迷糊糊的,起來以后就開始玩了會游戲淮椰,老爸回來以后...
    堅(jiān)志閱讀 236評論 0 0
  • 周末回媽媽家五慈,看到地里韭菜郁郁蔥蔥的,長得有一捺高了主穗,想著再有半個月就能吃上自家純綠色蔬菜了泻拦,心里挺高興。媽...
    曠懷素心閱讀 404評論 10 4