前面章节介绍了 IN 类型参数的存储过程的调用方式,本章节将介绍怎样调用带有 OUT 类型参数的存储过程。
(1)定义存储过程 proc_getUserName,SQL脚本如下:
CREATE PROCEDURE `proc_getUserName`(IN `userId` int,OUT `userName` varchar(100)) BEGIN select `name` into userName from `user` where user_id=userId; END
上面存储过程将根据 userId 获取用户名称,然后将名称存入 userName 参数,返回给程序。
(2)MyBatis 配置文件 mybatis-cfg.xml 内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="database.properties"/> <environments default="MySqlDatabase" > <environment id="MySqlDatabase" > <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/hxstrive/mybatis/procedure/demo2/UserMapper.xml" /> </mappers> </configuration>
(3)定义参数 Bean,ParamBean.java 文件代码如下:
package com.hxstrive.mybatis.procedure.demo2; public class ParamBean { private int userId; private String userName; // 忽略 getter和setter }
(4)定义 Mapper 接口,UserMapper.java 文件代码如下:
package com.hxstrive.mybatis.procedure.demo2; public interface UserMapper { /** * 获取用户名称 * @param paramBean */ void getUserName(ParamBean paramBean); }
(5)定义 Mapper XML 文件,UserMapper.xml 文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hxstrive.mybatis.procedure.demo2.UserMapper"> <parameterMap id="paramBeanMap" type="com.hxstrive.mybatis.procedure.demo2.ParamBean"> <parameter property="userId" mode="IN" jdbcType="INTEGER" /> <!-- 该参数用于接收查到的用户名称 --> <parameter property="userName" mode="OUT" jdbcType="VARCHAR" /> </parameterMap> <!-- 获取用户名称 --> <!-- statementType 设置处理该SQL语句的类型 --> <!-- CALLABLE - 使用JDBC的CallabledStatement类 --> <select id="getUserName" parameterMap="paramBeanMap" statementType="CALLABLE"> { call proc_getUserName( #{userId, mode=IN, jdbcType=INTEGER}, #{userName, mode=OUT, jdbcType=VARCHAR} ) } </select> </mapper>
(6)客户端代码如下:
package com.hxstrive.mybatis.procedure.demo2; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class ProcedureDemo { public static void main(String[] args) throws Exception { String cfgName = "com/hxstrive/mybatis/procedure/demo2/mybatis-cfg.xml"; InputStream input = Resources.getResourceAsStream(cfgName); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlFactory = factoryBuilder.build(input); SqlSession sqlSession = sqlFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); ParamBean paramBean = new ParamBean(); paramBean.setUserId(1); paramBean.setUserName(""); userMapper.getUserName(paramBean); System.out.println("userName=" + paramBean.getUserName()); } }
执行上面程序,执行成功后将在控制台输出用户名称。
注意:
存储过程类型为 OUT 的参数必须使用 JavaBean 来接收,不能直接使用 Java 基础类型来接收,如:java.lang.String、Integer,笔者亲测过!!