RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Mybatis中TypeHandler的作用是什么

Mybatis中TypeHandler的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联-专业网站定制、快速模板网站建设、高性价比浦城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式浦城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖浦城地区。费用合理售后完善,十载实体公司更值得信赖。

    mybatis-3.4.6.release.

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

    List-1

public interface TypeHandler {

  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中获取值,可以通过下标或列名称获取,此外存储过程的处理方式不同。

             Mybatis中TypeHandler的作用是什么

                                                              图1

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

    List-2

public class BigDecimalTypeHandler extends BaseTypeHandler

    BaseTypeHandler中使用了模板模式,具体实现由子类来实现,如下List-3:

    List-3 

public abstract class BaseTypeHandler extends TypeReference implements TypeHandler {

  @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 {

  @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);
  }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网站题目:Mybatis中TypeHandler的作用是什么
URL链接:http://scyingshan.cn/article/jeieop.html