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 |