jdbcType:定義了數據庫中的數據類型。
javaType:定義了java中的數據類型。
TypeHandler:承擔了jdbcType和javaType之間的互相轉換。
一、TypeHandler 接口源碼
/*
* TypeHandler接口
* @ BaseTypeHandler<T>
* */
public interface TypeHandler<T> {
//通過PreparedStatement設置SQL參數
void setParameter(PreparedStatement var1, int var2, T var3, JdbcType var4) throws SQLException;
//根據列名獲取結果集
T getResult(ResultSet var1, String var2) throws SQLException;
//根據下標獲取結果集
T getResult(ResultSet var1, int var2) throws SQLException;
//存儲過程
T getResult(CallableStatement var1, int var2) throws SQLException;
}
在mybatis中typeHandler都要(BaseTypeHander)實現org.apache.ibatis.type.TypeHandler接口,或接繼承BaseTypeHander。
二、BaseTypeHandler 抽象類源碼
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.apache.ibatis.type;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.session.Configuration;
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {
protected Configuration configuration;
public BaseTypeHandler() {}
public void setConfiguration(Configuration c) {
this.configuration = c;
}
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
// jdbcType 爲null是拋出異常,jdbcType不能爲空
throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
}
try {
// 設置空參
ps.setNull(i, jdbcType.TYPE_CODE);
} catch (SQLException var7) {
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: " + var7, var7);
}
} else {
try {
//setNonNullParameter是一個虛方法,需要BaseTypeHandler子類去實現
this.setNonNullParameter(ps, i, parameter, jdbcType);
} catch (Exception var6) {
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: " + var6, var6);
}
}
}
public T getResult(ResultSet rs, String columnName) throws SQLException {
Object result;
try {
//getNullableResult是一個虛方法,需要BaseTypeHandler子類去實現
result = this.getNullableResult(rs, columnName);
} catch (Exception var5) {
throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + var5, var5);
}
return rs.wasNull() ? null : result;
}
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
Object result;
try {
是一個虛方法,需要BaseTypeHandler子類去實現
result = this.getNullableResult(rs, columnIndex);
} catch (Exception var5) {
throw new ResultMapException("Error attempting to get column #" + columnIndex + " from result set. Cause: " + var5, var5);
}
return rs.wasNull() ? null : result;
}
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
Object result;
try {
//getNullableResult是一個虛方法,需要BaseTypeHandler子類去實現
result = this.getNullableResult(cs, columnIndex);
} catch (Exception var5) {
throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement. Cause: " + var5, var5);
}
return cs.wasNull() ? null : result;
}
//4個子類需要實現的虛方法
public abstract void setNonNullParameter(PreparedStatement var1, int var2, T var3, JdbcType var4) throws SQLException;
public abstract T getNullableResult(ResultSet var1, String var2) throws SQLException;
public abstract T getNullableResult(ResultSet var1, int var2) throws SQLException;
public abstract T getNullableResult(CallableStatement var1, int var2) throws SQLException;
}
三、StringTypeHandler源碼
public class StringTypeHandler extends BaseTypeHandler<String> {
public StringTypeHandler() {
}
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter);
}
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
mybatis內部實現的TypeHandler
typeHandler | javaType |
---|---|
NClobTypeHandler | string |
ClobReaderTypeHandler | Reader |
OffsetTimeTypeHandler | OffsetTime |
ByteObjectArrayTypeHandler | Byte[] |
DateOnlyTypeHandler | Date |
BlobTypeHandler | byte[] |
DateTypeHandler | Date |
IntegerTypeHandler | Integer |
SqlTimeTypeHandler | Time |
NStringTypeHandler | String |
CharacterTypeHandler | Character |
ArrayTypeHandler | Object |
EnumOrdinalTypeHandler | E extends Enum < E > |
StringTypeHandler | String |
BigDecimalTypeHandler | BigDecimal |
SqlTimestampTypeHandler | Timestamp |
BooleanTypeHandler | Boolean |
BlobInputStreamTypeHandler | InputStream |
BlobByteObjectArrayTypeHandler | Byte[] |
EnumTypeHandler | E extends Enum< E > |
MonthTypeHandler | Month |
FloatTypeHandler | Float |
ByteTypeHandler | Byte |
TimeOnlyTypeHandler | Date |
YearMonthTypeHandler | YearMonth |
InstantTypeHandler | Instant |
ObjectTypeHandler | Object |
ClobTypeHandler | String |
DoubleTypeHandler | Double |
ShortTypeHandler | Short |
LongTypeHandler | Long |
LocalDateTypeHandler | LocalDate |
UnknownTypeHandler | Object |
BigIntegerTypeHandler | BigInteger |
ByteArrayTypeHandler | byte[] |
OffsetDateTimeTypeHandler | OffsetDateTime |
JapaneseDateTypeHandler | JapaneseDate |
LocalDateTimeTypeHandler | LocalDateTime |
ZonedDateTimeTypeHandler | ZonedDateTime |
SqlDateTypeHandler | Date |
YearTypeHandler | Year |
LocalTimeTypeHandler | LocalTime |