Talk about the type conversion interface TypeHandler of Mybatis

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Talk about the type conversion interface TypeHandler of Mybatis

mybatis can realize the conversion between jdbc type and java type, specifically, there is a type converter interface:

TypeHandler

public interface TypeHandler<T> {

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

  /**
   * @param columnName Colunm name, when configuration <code>useColumnLabel</code> is <code>false</code>
   */
  T getResult(ResultSet rs, String columnName) throws SQLException;

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

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

}
复制代码

BaseTypeHandler implements the TypeHandler interface and implements the setParameter() method:

@Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
              + "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
              + "Try setting a different JdbcType for this parameter or a different configuration property. "
              + "Cause: " + e, e);
      }
    }
  }
复制代码

This method is to set the parameters of PreparedStatement, which is also a parameter binding, convert jdbcType to Java type, setNonNullParameter is an abstract method, and different classes implement this method according to different parameter types, such as the setNonNullParameter() method implemented by LongTypeHandler:

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType)
      throws SQLException {
    ps.setLong(i, parameter);
  }
复制代码

The getResult() method of BaseTypeHandler:

@Override
  public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
    try {
      return getNullableResult(cs, columnIndex);
    } catch (Exception e) {
      throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement.  Cause: " + e, e);
    }
  }
复制代码

The method is also relatively simple, directly calling the getNullableResult abstract class, the function is to obtain data from the ResultSet, convert the Java type to the JdbcType type, such as getNullableResult() implemented by LongTypeHandler:

@Override
  public Long getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
    long result = rs.getLong(columnName);
    return result == 0 && rs.wasNull() ? null : result;
  }
复制代码

Through source code analysis, we know that the role of the TypeHandler interface is to implement type conversion. Mybatis obtains TypeHandler when it is initialized, and then creates a TypeHandler instance and registers it in the TypeHandlerRegistry. These instances are managed by the TypeHandlerRegistry, which we will introduce in the next article. Take a look at the TypeHandlerRegistry class

Summarize

This article talks about the type conversion interface TypeHandler of Mybatis and its implementation class BaseTypeHandler. The type conversion interface is obviously to realize the conversion between jdbc type and java type. At the same time, it analyzes the setParameter() method and getResult() method of BaseTypeHandler, getNullableResult It is an abstract class, and concrete methods are implemented by other implementation classes.

Guess you like

Origin juejin.im/post/7079375930979778568