前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis之TypeHandler

Mybatis之TypeHandler

作者头像
克虏伯
修改2019-10-22 10:28:30
9170
修改2019-10-22 10:28:30
举报

    mybatis-3.4.6.release.

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

List-1

代码语言:javascript
复制
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

代码语言:javascript
复制
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> 

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

List-3

代码语言:javascript
复制
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

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档