Mybatis之TypeHandler

自古美人都是妖i 提交于 2021-02-16 19:35:38

    mybatis-3.4.6.release.

    TypeHandler在mybatis中是个重要的组件,对statement设置参数还是从Resultset中取值,都会用到它。

    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中使用到,其它三个getResult方法在ResultSetHandler中使用到,为什么会有三个getResult方法是因为从ResultSet中获取值,可以通过下标或列名称获取,此外存储过程的处理方式不同。

             

                                                              图1

    BaseTypeHandler的类继承图如图1所示,先来看下TypeReferernce,其作用主要是获取类上的泛型,在TypeReferernce的构造方法中实现的,如下List-2所示,getRawType的结果是BigDecimal.

    List-2

public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> 

    BaseTypeHandler中使用了模板模式,具体实现由子类来实现,如下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,则直接调用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;

}

    来看个例子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);
  }
}

Reference

  1. https://github.com/mybatis/mybatis-3/tree/3.4.x
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!