这一篇博客我们学习一下Mybatis调用存储过程的使用和运行流程。首先我们先创建一个简单的存储过程
DELIMITER $这个存储过程的含义其实比较简单的,就是输入age,然后执行select count(*) from users where users.age = age into user_count;获得年龄等于age的人数赋值给user_count,还是比较简单的。
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
$
接下来是存储过程的调用,执行如下命令就可以完成存储过程的调用。
接下来我们看看利用Mybatis是如何调用存储过程的。
userMapper.xml添加存储过程调用配置:
<select id="count" statementType="CALLABLE" parameterMap="getUserCountMap"> CALL mybatis.ges_user_count(?,?)</select>
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); } }