上一篇讲了StatementHandler,其中有ParameterHandler(参数处理器)是在StatementHandler被创建时被创建的。下面对ParameterHandler进行说明。其代码如下:

public interface ParameterHandler {
Object getParameterObject();
void setParameters(PreparedStatement ps) throws SQLException;
}

它只有两个方法,其中getParameterObject()是获取参数的,而setParameters()是设置参数的。

ParameterHandler只有一个实现类DefaultParameterHandler,在查看DefaultParameterHandler代码之前首先了解DefaultParameterHandler中的相关属性。代码如下所示:

//类型转换处理器:映射Java中数据类型与数据库中数据类型对应关系
private final TypeHandlerRegistry typeHandlerRegistry;
//映射sql语句与数据库操作对象关系以及sql关联的sql标签信息
private final MappedStatement mappedStatement;
//存储的需要进行赋值参数内容
private final Object parameterObject;
//输送的具体的sql语句
private final BoundSql boundSql;
//包含myBatis框架核心配置文件信息和sql映射文件信息
private final Configuration configuration;

下面查看DefaultParameterHandler中setParameters()的实现,代码如下所示:

  public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
//// parameterMappings是对#{}里给出的参数信息的封装,即这个SQL是个参数化SQL时会存在(SQL语句中带占位符?)
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
//存储过程才存在输出参数,所以当参数不是输出参数的时候,就需要设置
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
//这里的propertyName对应的是一个additionalParameters Map对象经过封装的key值,而不是Bean的属性
String propertyName = parameterMapping.getProperty();
//判断propertyName是additionalParameters中的key
if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
}
//如果在typeHandlerRegistry中已经注册了这个参数的Class对象,value直接等于Method传进来的参数
else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
}
//否则是Map或Bean,List或Array被封装成了Map,通过Key得到Value;Bean是通过封装的MataObject对象,Bean通过反射得到,再用propertyName得到相应的Value。
else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
//得到parameterMapping的TypeHandler,均为IntegerTypeHandler
TypeHandler typeHandler = parameterMapping.getTypeHandler();
//得到相应parameterMapping的JdbcType,如果没有在#{}中显式的指定JdbcType,则为null
JdbcType jdbcType = parameterMapping.getJdbcType();
//当value和jdbcType都为null执行执行setNull操作时,会报OTHER的错误,因此指定JdbcType
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
try {
//具体实现ps.set***
typeHandler.setParameter(ps, i + 1, value, jdbcType);
} catch (TypeException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
} catch (SQLException e) {
throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
}
}
}
}
}

上面的的setParameter()是在BaseTypeHandler中实现的,它是IntegerTypeHandler等的父类,这里采用了模板模式。setParameter()的代码如下:

  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 {
//执行PreparedStatement的setNull方法
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);
}
}
}

下面对上面所说的setNull()进行说名:

public void setNull(int parameterIndex, int sqlType) throws SQLException{}
//parameterIndex:整形参数,第一个为1,第二个为2...
//sqlType必须为java.sql.Types 中定义的SQL类型代码
//若parameterIndex 不对应于 SQL 语句中的参数标记;发生数据库访问错误;或在关闭的 PreparedStatement上调用此方法,会抛出SQLException异常。

由上面代码知,当SQL类型代码为1111(OTHER)时,就会出现报错:java.sql.SQLException: 无效的列类型: 1111,解决方案是在其后加上jdbcType,示例代码如下:

<insert id="insertUser" parameterType="com.luis.domain.User">
insert into t_user(s_id,name,age)
values (
#{SId,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER}
);
</insert>

另有博主验证在ibatis2 可以正常的执行 数据库可以正常的插入数据。

参见:http://www.cnblogs.com/panxuejun/p/6163779.html

SqlSession对象之ParameterHandler的更多相关文章

  1. SqlSession对象之StatementHandler

    上一篇讲了SqlSession对象中的Executor,接下来将对SqlSession的另一个对象StatementHandler进行讲解. 一.StatementHandler介绍 Statemen ...

  2. mybatis的两个核心对象SqlSessionFactory和SqlSession对象

    mybatis的两个核心对象SqlSessionFactory和SqlSession对象 参见:https://www.cnblogs.com/wxdestiny/p/9743686.html

  3. 对 Service中sqlsession对象的优化

    在本线程中添加object数据,必须在本线程中才能获取出来..其他线程获取不到. public class Test { public static void main(String[] args) ...

  4. SqlSession对象之Executor

    Executor是Mybatis的一个核心接口,每一个SqlSession对象都会拥有一个Executor(执行器对象):这个执行对象负责[增删改查]的具体操作,我们可以简单的将它理解为JDBC中St ...

  5. 使用ThreadLocal管理Mybatis中SqlSession对象

    转自http://blog.csdn.net/qq_29227939/article/details/52029065 public class MybatisUtil { private stati ...

  6. SqlSession对象之ResultSetHandler

    ResultSetHandler是Mybatis中的另一重要接口,它的代码如下所示: public interface ResultSetHandler { <E> List<E&g ...

  7. 关于Mybatis与Spring整合之后SqlSession与mapper对象之间数量的问题。

    1,sqlsession的真实类型和数量 由于使用spring管理bean,当我们在代码中需要使用这个bean的时候,会首先去容器中找,第一次需要调用MapperFactoryBean的getObje ...

  8. MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用

    学习框架一个比较好的路径阅读源码.本文介绍的SqlSessionFactory和SqlSession.可以通过了解SqlSessionFactory接口和SqlSession接口以及两个的实现类入手, ...

  9. MyBatis的2个核心对象:SqlSessionFactory、SqlSession

    SqlSessionFactory SqlSessionFactory是单个数据库映射关系经过编译后的内存镜像,主要作用是创建SqlSession. InputStream inputStream = ...

随机推荐

  1. [leetcode.com]算法题目 - Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. google breakpad for linux(2)

    breakpad 是什么 breakpad 是一个包含了一系列库文件和工具的开源工具包,使用它可以帮助我们在程序崩溃后进行一系列的后续处理,如现场的保存(core dump),及事后分析(重建 cal ...

  3. kali linux 安装sublime text3完全教程

    点击进入官网 下载页面 将鼠标放在64 bit(64位系统)上右击复制链接 打开终端: #wget 路径(粘贴刚复制的) #tar -xvvf 刚刚下载的文件文件名(解压) #mv 解压出来的文件名  ...

  4. mongoose入门

    概述 像Mysql和Mongodb这样的数据库,一般都是在命令行或者工具里面进行操作,如果想在node搭建的服务器上面操作,就必须要利用特殊的模块的.其中操作Mongodb数据库需要用到mongoos ...

  5. Swift5 语言参考(五) 语句

    在Swift中,有三种语句:简单语句,编译器控制语句和控制流语句.简单语句是最常见的,由表达式或声明组成.编译器控制语句允许程序更改编译器行为的各个方面,并包括条件编译块和行控制语句. 控制流语句用于 ...

  6. Django模版结构优化和加载静态文件

    引入模版 有时候一些代码是在许多模版中都用到的.如果我们每次都重复的去拷贝代码那肯定不符合项目的规范.一般我们可以把这些重复性的代码抽取出来,就类似于Python中的函数一样,以后想要使用这些代码的时 ...

  7. oralce11g RAC 启动后 CRS-0184: Cannot communicate with the CRS daemon.

    很奇怪的一个问题! ORACLE数据库服务器,系统启动之后,查看集群状态,发现CRS实例不可用,然后网上查找资料: 隔了几分钟之后,再次查询相关集群服务状态,发现正常了!!! 暂时得出的结论:操作系统 ...

  8. 以太坊ERC20代币合约案例

    一.ERC20代币合约与web3调用 ERC20代币合约在小白看来觉得很高大上,但其实就是一个代币的定义标准,方便其他dapp统一调用各种代币的方法.如图: 二.ERC20合约标准 [官方链接] co ...

  9. Android众说纷纭分辨率

    Andoid最被人诟病的就是显示屏的各种不同尺寸和不同分辨率.由于Android厂商的纷繁多样,导致出现了不同尺寸和不同分辨率的手机,指示开发者需要兼容各种手机屏幕.本文想学习的就是Android的显 ...

  10. windows线程退出的方法

    线程的handle用处: 线程的handle是指向“线程的内核对象”的,而不是指向线程本身.每个内核对象只是内核分配的一个内存块,并且只能由内核访问.该内存块是一种数据结构,它的成员负责维护对象的各种 ...