Mybatis——TypeHandler 类型转换器

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

typeHandlerjavaType
NClobTypeHandlerstring
ClobReaderTypeHandlerReader
OffsetTimeTypeHandlerOffsetTime
ByteObjectArrayTypeHandlerByte[]
DateOnlyTypeHandlerDate
BlobTypeHandlerbyte[]
DateTypeHandlerDate
IntegerTypeHandlerInteger
SqlTimeTypeHandlerTime
NStringTypeHandlerString
CharacterTypeHandlerCharacter
ArrayTypeHandlerObject
EnumOrdinalTypeHandlerE extends Enum < E >
StringTypeHandlerString
BigDecimalTypeHandlerBigDecimal
SqlTimestampTypeHandlerTimestamp
BooleanTypeHandlerBoolean
BlobInputStreamTypeHandlerInputStream
BlobByteObjectArrayTypeHandlerByte[]
EnumTypeHandlerE extends Enum< E >
MonthTypeHandlerMonth
FloatTypeHandlerFloat
ByteTypeHandlerByte
TimeOnlyTypeHandlerDate
YearMonthTypeHandlerYearMonth
InstantTypeHandlerInstant
ObjectTypeHandlerObject
ClobTypeHandlerString
DoubleTypeHandlerDouble
ShortTypeHandlerShort
LongTypeHandlerLong
LocalDateTypeHandlerLocalDate
UnknownTypeHandlerObject
BigIntegerTypeHandlerBigInteger
ByteArrayTypeHandlerbyte[]
OffsetDateTimeTypeHandlerOffsetDateTime
JapaneseDateTypeHandlerJapaneseDate
LocalDateTimeTypeHandlerLocalDateTime
ZonedDateTimeTypeHandlerZonedDateTime
SqlDateTypeHandlerDate
YearTypeHandlerYear
LocalTimeTypeHandlerLocalTime
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值