Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Mybatis type conversion interface TypeHandler

Mybatis can convert between JDBC types and Java types. Specifically, there is an interface for type converters:

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;

}
Copy the code

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); }}}Copy the code

SetNonNullParameter is an abstract method. There are different classes that implement this method depending on the parameter type. For example, the setNonNullParameter() method implemented by LongTypeHandler:

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType)
      throws SQLException {
    ps.setLong(i, parameter);
  }
Copy the code

BaseTypeHandler’s getResult() method:

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

LongTypeHandler getNullableResult(); LongTypeHandler getNullableResult(); getNullableResult();

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

Through source code analysis, we know that the function of TypeHandler interface is used to achieve type conversion. Mybatis obtains TypeHandler when initialization, and then creates TypeHandler instance to register in TypeHandlerRegistry. These instances are managed by The TypeHandlerRegistry class, which we’ll introduce in the next article

conclusion

Mybatis: TypeHandler, BaseTypeHandler, BaseTypeHandler, BaseTypeHandler, BaseTypeHandler At the same time, the setParameter() method and getResult() method of BaseTypeHandler are analyzed. GetNullableResult is an abstract class, and the concrete methods are implemented by other implementation classes.