オファーが届きました。友達を掘って受け取りましょう!私は2022年の春の採用チェックインイベントに参加しています。クリックしてイベントの詳細を表示します。
Mybatisの型変換インターフェースTypeHandlerについて話す
mybatisは、jdbcタイプとjavaタイプの間の変換を実現できます。具体的には、型コンバーターインターフェイスがあります。
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は、TypeHandlerインターフェイスを実装し、setParameter()メソッドを実装します。
@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);
}
}
}
复制代码
このメソッドは、パラメーターバインディングでもあるPreparedStatementのパラメーターを設定し、jdbcTypeをJavaタイプに変換し、setNonNullParameterは抽象メソッドであり、さまざまなクラスが、によって実装されるsetNonNullParameter()メソッドなどのさまざまなパラメータータイプに従ってこのメソッドを実装します。 LongTypeHandler:
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType)
throws SQLException {
ps.setLong(i, parameter);
}
复制代码
BaseTypeHandlerのgetResult()メソッド:
@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);
}
}
复制代码
このメソッドも比較的単純で、getNullableResult抽象クラスを直接呼び出します。関数は、ResultSetからデータを取得し、JavaタイプをLongTypeHandlerによって実装されるgetNullableResult()などのJdbcTypeタイプに変換します。
@Override
public Long getNullableResult(ResultSet rs, String columnName)
throws SQLException {
long result = rs.getLong(columnName);
return result == 0 && rs.wasNull() ? null : result;
}
复制代码
ソースコード分析により、TypeHandlerインターフェースの役割は型変換を実装することであることがわかります。Mybatisは初期化時にTypeHandlerを取得し、TypeHandlerインスタンスを作成して、TypeHandlerRegistryに登録します。これらのインスタンスはTypeHandlerRegistryによって管理されます。次の記事で紹介します。TypeHandlerRegistryクラスを見てください。
要約する
この記事では、Mybatisの型変換インターフェースTypeHandlerとその実装クラスBaseTypeHandlerについて説明します。型変換インターフェースは、明らかにjdbc型とjava型の間の変換を実現するためのものです。同時に、setParameter()メソッドとgetResult()を分析します。 BaseTypeHandlerのメソッドgetNullableResultこれは抽象クラスであり、具体的なメソッドは他の実装クラスによって実装されます。