千家信息网

Mybatis中TypeHandler的作用是什么

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,Mybatis中TypeHandler的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。mybatis-3.4.
千家信息网最后更新 2025年02月02日Mybatis中TypeHandler的作用是什么

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

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

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

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

0