ParameterHandler接口是参数处理器,位于mybatis包的org.apache.ibatis.executor.parameter下,源码如下:

 public interface ParameterHandler {

 Object getParameterObject();//获取参数

 void setParameters(PreparedStatement ps)//设置参数
throws SQLException; }

可见ParameterHandler接口只有简单的两个方法,一个是获取参数一个是设置参数。ParameterHandler接口默认实现类是DefaultParameterHandler,主要源码如下:

 public class DefaultParameterHandler implements ParameterHandler {

 private final TypeHandlerRegistry typeHandlerRegistry;

 private final MappedStatement mappedStatement;
private final Object parameterObject;
private BoundSql boundSql;
private Configuration configuration; public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
this.mappedStatement = mappedStatement;
this.configuration = mappedStatement.getConfiguration();
this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
this.parameterObject = parameterObject;//设置参数
this.boundSql = boundSql;
} @Override
public Object getParameterObject() {
return parameterObject;//返回参数
} @Override
public void setParameters(PreparedStatement ps) {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();//获取所有参数,ParameterMapping是jdbc和java类型之间的对应关系
if (parameterMappings != null) {
//遍历所有参数,将java 类型设置成jdbc类型
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params
value = boundSql.getAdditionalParameter(propertyName);
} else if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
value = metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
try {
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);
}
}
}
}
} }

而ParameterHandler的初始化同样也是在Configuration中实现的,代码如下:

   public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}

mybatis源码解析11---ParameterHandler解析的更多相关文章

  1. MyBatis 源码分析 - 映射文件解析过程

    1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...

  2. Spring mybatis源码篇章-MybatisDAO文件解析(二)

    前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(一) 默认加载mybatis主文件方式 XMLConfigBuilder ...

  3. Spring mybatis源码篇章-MybatisDAO文件解析(一)

    前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 加载指定的mybatis主文件 Mybatis模板文件,其中的属性 ...

  4. Mybatis 源码之Plugin类解析

    public class Plugin implements InvocationHandler { private Object target; //目标对象 private Interceptor ...

  5. MyBatis源码部分简单地解析

    . 一.解析xml: > org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream, java.l ...

  6. MyBatis 源码分析 - 配置文件解析过程

    * 本文速览 由于本篇文章篇幅比较大,所以这里拿出一节对本文进行快速概括.本篇文章对 MyBatis 配置文件中常用配置的解析过程进行了较为详细的介绍和分析,包括但不限于settings,typeAl ...

  7. Mybatis源码解析优秀博文

    最近阅读了许久的mybatis源码,小有所悟.同时也发现网上有许多优秀的mybatis源码讲解博文.本人打算把自己阅读过的.觉得不错的一些博文列出来.以此进一步加深对mybatis框架的理解.其实还有 ...

  8. MyBatis 源码分析 - 插件机制

    1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...

  9. Spring mybatis源码学习指引目录

    前言: 分析了很多方面的mybatis的源码以及与spring结合的源码,但是难免出现错综的现象,为了使源码陶冶更为有序化.清晰化,特作此随笔归纳下分析过的内容.博主也为mybatis官方提供过pul ...

  10. MyBatis 源码分析 - 缓存原理

    1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 Redis 或 memcached 等缓存中间件,拦截大量奔向数据库的请求,减轻数据库压力.作为一个重要的组件,MyBatis 自然 ...

随机推荐

  1. sql 同步2个表中的一个字段数据

    update PMS.tenant_contract a inner join(select id,home_id from PMS.owner_contract) c on a.id = c.id ...

  2. Laravel Homestead 离线安装

    一.写在之前,网络不够快想要安装Homestead,也是一个浩大的工程,对于下载一个 1.22G左右的 laravel/homestead box 也是非常的麻烦.那么如何才能离线安装呢? 接着往下看 ...

  3. [破解] IPhone 5S icloud dns bypass

    http://ui.iclouddnsbypass.com/deviceservices/buddy/barney_activation_help_en_us.buddyml http://www.j ...

  4. common lisp 里的几个操作符(2)

    集合 (Set) member 函数 默认使用 eql比较对象,可传入关键字参数 :test,作为比较的函数.关键字参数 :key,指定在每个元素上应用这个函数. > (member 2 '(( ...

  5. python 获取本机ip

    有的设备上,有host相关配置,或者网络环境特殊,导致从hostname获取ip不符合预期. 如下函数整合了多种获取ip的方式,大部分情况都可涵盖,一般情况下可以满足需求~ 核心是创建一个UDP协议下 ...

  6. IIS多个应用程序共享Session

    在应用程序目录下面添加一个Global.asax文件,在文件中添加以下代码: public override void Init() { base.Init(); foreach (string mo ...

  7. 概率DP求解例题

    1,逆推状态:山东省赛2013年I题 Problem I: The number of steps Description Mary stands in a strange maze, the maz ...

  8. 洛谷试炼场 - 关卡1-5 - 简单字符串 - (Done)

    P1055 ISBN号码 #include<bits/stdc++.h> using namespace std; string s; ]={','X'}; int main() { ci ...

  9. Golang go get第三方库的坑

    在树莓派上go get fail的问题记录及解决方案 go get github.com/terrancewong/serial # 错误为GOPATH路径的问题 cannot find packag ...

  10. I2C驱动框架 (kernel-3.4.2)

    先用韦老师的图: 注:  新版本内核的i2c驱动框架采用了    i2c_client -------> i2c_bus_type  <-------- i2c_driver   框架 如 ...