欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis的類型轉(zhuǎn)換接口TypeHandler

 更新時(shí)間:2022年08月09日 14:46:18   作者:周杰倫本人  
這篇文章主要介紹了Mybatis的類型轉(zhuǎn)換接口TypeHandler,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

前言:

mybatis可以實(shí)現(xiàn)jdbc類型和java類型之間的轉(zhuǎn)換,具體來說有一個(gè)類型轉(zhuǎn)換器的接口

類型處理器接口TypeHandler

public interface TypeHandler<T> {

  void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;

  /**
   * @param columnName Colunm name, when configuration <code>useColumnLabel</code> is <code>false</code>
   */
  T getResult(ResultSet rs, String columnName) throws SQLException;

  T getResult(ResultSet rs, int columnIndex) throws SQLException;

  T getResult(CallableStatement cs, int columnIndex) throws SQLException;

}

類型處理器BaseTypeHandler

jdbc類型轉(zhuǎn)為java類型

BaseTypeHandler實(shí)現(xiàn)了TypeHandler接口,實(shí)現(xiàn)了setParameter()方法:

@Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
              + "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different configuration property. "
              + "Cause: " + e, e);
      }
    }
  }

這個(gè)方法中就是設(shè)置PreparedStatement的參數(shù),也是參數(shù)綁定,將jdbcType轉(zhuǎn)為Java類型,setNonNullParameter是抽象方法,根據(jù)不同的參數(shù)類型有不同的類實(shí)現(xiàn)了這個(gè)方法,比如

LongTypeHandler實(shí)現(xiàn)的setNonNullParameter()方法:

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType)
      throws SQLException {
    ps.setLong(i, parameter);
  }

java類型轉(zhuǎn)為jdbc類型

BaseTypeHandler的getResult()方法:

@Override
  public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
    try {
      return getNullableResult(cs, columnIndex);
    } catch (Exception e) {
      throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement.  Cause: " + e, e);
    }
  }

方法也比較簡(jiǎn)單,直接調(diào)用了getNullableResult抽象類,功能就是從ResultSet中獲取數(shù)據(jù),把Java類型轉(zhuǎn)換為JdbcType類型,比如LongTypeHandler實(shí)現(xiàn)的getNullableResult():

@Override
  public Long getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
    long result = rs.getLong(columnName);
    return result == 0 && rs.wasNull() ? null : result;
  }

通過源碼分析我們知道了TypeHandler接口的作用就是用來實(shí)現(xiàn)類型轉(zhuǎn)換的,mybatis在初始化的時(shí)候就獲取TypeHandler,然后創(chuàng)建TypeHandler實(shí)例注冊(cè)到TypeHandlerRegistry中,由TypeHandlerRegistry來進(jìn)行管理這些實(shí)例,下篇文章中我們介紹一下TypeHandlerRegistry這個(gè)類

總結(jié)

文章講了Mybatis的類型轉(zhuǎn)換接口TypeHandler和它的實(shí)現(xiàn)類BaseTypeHandler,類型轉(zhuǎn)換接口顯然就是實(shí)現(xiàn)jdbc類型和java類型之間的轉(zhuǎn)換,同時(shí)分析了BaseTypeHandler的setParameter()方法和getResult()方法,getNullableResult是抽象類,具體方法由其他實(shí)現(xiàn)類進(jìn)行實(shí)現(xiàn)。

到此這篇關(guān)于Mybatis的類型轉(zhuǎn)換接口TypeHandler的文章就介紹到這了,更多相關(guān)Mybatis類型轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論