这一篇博客我们学习一下Mybatis调用存储过程的使用和运行流程。首先我们先创建一个简单的存储过程

DELIMITER $
CREATE PROCEDURE mybatis.ges_user_count(IN age INT, OUT user_count INT)
BEGIN
SELECT COUNT(*) FROM users WHERE users.age=age INTO user_count;
END
$

这个存储过程的含义其实比较简单的,就是输入age,然后执行select count(*) from users where users.age = age into user_count;获得年龄等于age的人数赋值给user_count,还是比较简单的。

接下来是存储过程的调用,执行如下命令就可以完成存储过程的调用。

接下来我们看看利用Mybatis是如何调用存储过程的。

userMapper.xml添加存储过程调用配置:

<select id="count" statementType="CALLABLE" parameterMap="getUserCountMap">
      CALL mybatis.ges_user_count(?,?)
</select>

Main函数:

public class Learn1Main {

	public static void main(String [] args){

		//mybatis的配置文件
	    String resource = "learn/mybatis-config.xml";
	    //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
	    InputStream is = Learn1Main.class.getClassLoader().getResourceAsStream(resource);
	    //构建sqlSession的工厂
	    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	    SqlSession session = sessionFactory.openSession();
	    Map<String, Integer> parameterMap = new HashMap<String, Integer>();
	    parameterMap.put("age", 12);
	    parameterMap.put("user_count", -1);
	    session.selectOne("com.tianjunwei.learn.learn1.entity.User.count", parameterMap);
	    Integer result = parameterMap.get("user_count");
	    System.out.println(result);

	}
}

运行结果:

其最终的执行过程在DefaultResultSetHandler中,调用普通的sql和存储过程之间还是有所区别的,Sql语句的执行是使用CallableStatement。

Mybatis源码之Statement处理器CallableStatementHandler(六)

  //
  // HANDLE OUTPUT PARAMETER
  //
  //调用存储过程返回结果,将结果值放在参数中
  @Override
  public void handleOutputParameters(CallableStatement cs) throws SQLException {
    final Object parameterObject = parameterHandler.getParameterObject();
    final MetaObject metaParam = configuration.newMetaObject(parameterObject);
    final List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
	//循环处理每个参数
    for (int i = 0; i < parameterMappings.size(); i++) {
      final ParameterMapping parameterMapping = parameterMappings.get(i);
	  //判断参数的模式
      if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) {
        if (ResultSet.class.equals(parameterMapping.getJavaType())) {
          handleRefCursorOutputParameter((ResultSet) cs.getObject(i + 1), parameterMapping, metaParam);
        } else {
          final TypeHandler<?> typeHandler = parameterMapping.getTypeHandler();
          metaParam.setValue(parameterMapping.getProperty(), typeHandler.getResult(cs, i + 1));
        }
      }
    }
  }

  private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException {
    try {
      final String resultMapId = parameterMapping.getResultMapId();
      final ResultMap resultMap = configuration.getResultMap(resultMapId);
      final DefaultResultHandler resultHandler = new DefaultResultHandler(objectFactory);
      final ResultSetWrapper rsw = new ResultSetWrapper(rs, configuration);
      handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null);
      metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList());
    } finally {
      // issue #228 (close resultsets)
      closeResultSet(rs);
    }
  }

Mybatis源码分析之存储过程调用的更多相关文章

  1. mybatis源码分析(方法调用过程)

    十一月月底,宿舍楼失火啦,搞得20多天没有网,目测直到放假也不会来了... 正题 嗯~,其实阅读源码不是为了应付面试,更重要的让你知道,大师是怎样去写代码的,同样是用Java,为啥Clinton Be ...

  2. MyBatis源码分析-MyBatis初始化流程

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...

  3. MyBatis源码分析-SQL语句执行的完整流程

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...

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

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

  5. Mybatis源码分析

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...

  6. 精尽MyBatis源码分析 - MyBatis 的 SQL 执行过程(一)之 Executor

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  7. 精尽MyBatis源码分析 - SQL执行过程(二)之 StatementHandler

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  8. 精尽MyBatis源码分析 - SQL执行过程(三)之 ResultSetHandler

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  9. 精尽 MyBatis 源码分析 - SqlSession 会话与 SQL 执行入口

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

随机推荐

  1. bzoj 4180: 字符串计数

    Description SD有一名神犇叫做Oxer,他觉得字符串的题目都太水了,于是便出了一道题来虐蒟蒻yts1999. 他给出了一个字符串T,字符串T中有且仅有4种字符 'A', 'B', 'C', ...

  2. 12563 Jin Ge Jin Qu hao

    • Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...

  3. 例10-2 uva12169(扩展欧几里得)

    题意:已知xi=(a*xi-1+b) mod 10001,且告诉你x1,x3.........x2*t-1,让你求出其偶数列 思路: 枚举a,然后通过x1,x3求出b,再验证是否合适 1.设a, b, ...

  4. Frame buffer分析 - fbmem.c【转】

    转自:http://www.cnblogs.com/armlinux/archive/2012/03/01/2396753.html 45 struct fb_info *registered_fb[ ...

  5. django rest-framework 1.序列化 一

    上图为项目完整的目录结构 一.入门 需要先安装需要使用到包 pip install django pip install djangorestframework 先来创建一个新项目 django-ad ...

  6. VC++6.0的使用方法

    VC++6.0的最基本使用方法,创建一个c++项目工程可参考:https://jingyan.baidu.com/article/8ebacdf0cbdb5749f75cd54a.html 这里面的操 ...

  7. Python与C的简单比较(Python3.0)

    Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...

  8. decode-ways(动态规划)

    题目描述 A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A ...

  9. ⏰Day.js 2kB超轻量时间库 和Moment.js一样的API

    Moment.js 是一个大而全的 JS 时间库,很大地方便了我们处理日期和时间.但是 Moment.js太重了(200k+ with locals),可能一般项目也只使用到了她几个常用的API.虽然 ...

  10. 去掉Bootstrap fileinput缩略图上面的上传删除按钮?