Mybatis中TypeHandler的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
mybatis-3.4.6.release.
TypeHandler在mybatis中是个重要的组件,对statement设置参数还是从Resultset中取值,都会用到它。
List-1
public interface TypeHandler<T> {
void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
T getResult(ResultSet rs, String columnName) throws SQLException;
T getResult(ResultSet rs, int columnIndex) throws SQLException;
T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}
如List-1中所示,setParameter方法在ParameterHandler中使用到,其它三个getResult方法在ResultSetHandler中使用到,为什么会有三个getResult方法是因为从ResultSet中获取值,可以通过下标或列名称获取,此外存储过程的处理方式不同。
图1
BaseTypeHandler的类继承图如图1所示,先来看下TypeReferernce,其作用主要是获取类上的泛型,在TypeReferernce的构造方法中实现的,如下List-2所示,getRawType的结果是BigDecimal.
List-2
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal>
BaseTypeHandler中使用了模板模式,具体实现由子类来实现,如下List-3:
List-3
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {
@Override
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
...
如果parameter是null,则直接调用PreparedStatement的setNull方法
ps.setNull(i, jdbcType.TYPE_CODE);
...
setNonNullParameter(ps, i, parameter, jdbcType);
...
}
@Override
public T getResult(ResultSet rs, String columnName) throws SQLException {
...
result = getNullableResult(rs, columnName);
...
}
@Override
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
...
result = getNullableResult(rs, columnIndex);
...
}
@Override
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
...
result = getNullableResult(cs, columnIndex);
...
}
public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;
public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
}
来看个例子BooleanTypeHandler:
List-4
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
throws SQLException {
ps.setBoolean(i, parameter);
}
@Override
public Boolean getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getBoolean(columnName);
}
@Override
public Boolean getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getBoolean(columnIndex);
}
@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getBoolean(columnIndex);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/2518341/blog/3119789