溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Mybatis中TypeHandler的作用是什么

發(fā)布時(shí)間:2021-06-18 17:57:01 來(lái)源:億速云 閱讀:299 作者:Leah 欄目:大數(shù)據(jù)

Mybatis中TypeHandler的作用是什么,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

    mybatis-3.4.6.release.

    TypeHandler在mybatis中是個(gè)重要的組件,對(duì)statement設(shè)置參數(shù)還是從Resultset中取值,都會(huì)用到它。

    List-1

public interface TypeHandler<T> {

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

  T getResult(ResultSet rs, String columnName) throws SQLException;

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

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

}

    如List-1中所示,setParameter方法在ParameterHandler中使用到,其它三個(gè)getResult方法在ResultSetHandler中使用到,為什么會(huì)有三個(gè)getResult方法是因?yàn)閺腞esultSet中獲取值,可以通過(guò)下標(biāo)或列名稱(chēng)獲取,此外存儲(chǔ)過(guò)程的處理方式不同。

             Mybatis中TypeHandler的作用是什么

                                                              圖1

    BaseTypeHandler的類(lèi)繼承圖如圖1所示,先來(lái)看下TypeReferernce,其作用主要是獲取類(lèi)上的泛型,在TypeReferernce的構(gòu)造方法中實(shí)現(xiàn)的,如下List-2所示,getRawType的結(jié)果是BigDecimal.

    List-2

public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal>

    BaseTypeHandler中使用了模板模式,具體實(shí)現(xiàn)由子類(lèi)來(lái)實(shí)現(xiàn),如下List-3:

    List-3 

public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {

  @Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
      ...
      如果parameter是null,則直接調(diào)用PreparedStatement的setNull方法
      ps.setNull(i, jdbcType.TYPE_CODE);
      ...
      setNonNullParameter(ps, i, parameter, jdbcType);
      ...
  }

  @Override
  public T getResult(ResultSet rs, String columnName) throws SQLException {
      ...
      result = getNullableResult(rs, columnName);
      ...
  }

  @Override
  public T getResult(ResultSet rs, int columnIndex) throws SQLException {
      ...
      result = getNullableResult(rs, columnIndex);
      ...
  }

  @Override
  public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
      ...
      result = getNullableResult(cs, columnIndex);
      ...
  }

  public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;

  public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;

}

    來(lái)看個(gè)例子BooleanTypeHandler:

    List-4

public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {

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

  @Override
  public Boolean getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
    return rs.getBoolean(columnName);
  }

  @Override
  public Boolean getNullableResult(ResultSet rs, int columnIndex)
      throws SQLException {
    return rs.getBoolean(columnIndex);
  }

  @Override
  public Boolean getNullableResult(CallableStatement cs, int columnIndex)
      throws SQLException {
    return cs.getBoolean(columnIndex);
  }
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI