前面章节分别介绍了怎样调用拥有 IN 和 OUT 类型参数的存储过程,本章节将介绍 INOUT 类型参数的存储过程调用方法。
下面实例将使用 userId 获取用户的年龄,然后将年龄放到 userId 中返回。
(1)定义存储过程 proc_getUserAge,SQL 脚本如下:
CREATE PROCEDURE `proc_getUserAge`(INOUT `userId` int) BEGIN select age into userId from `user` where user_id=userId; END
上面过程将根据 userId 查询用户年龄,然后将查询到的用户年龄放到 userId 参数。
(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/demo3/UserMapper.xml" /> </mappers> </configuration>
(3)定义参数Bean ParamBean.java,代码如下:
package com.hxstrive.mybatis.procedure.demo3; public class ParamBean { private Integer userId; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } }
(4)定义 Mapper 接口 UserMapper.java,代码如下:
package com.hxstrive.mybatis.procedure.demo3; public interface UserMapper { /** * 获取用户年龄 * @param param */ void getUserAge(ParamBean param); }
(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.demo3.UserMapper"> <parameterMap id="paramBeanMap" type="com.hxstrive.mybatis.procedure.demo3.ParamBean"> <parameter property="userId" mode="INOUT" jdbcType="INTEGER" /> </parameterMap> <!-- 获取用户名称 --> <!-- statementType 设置处理该SQL语句的类型 --> <!-- CALLABLE - 使用JDBC的CallabledStatement类 --> <select id="getUserAge" parameterMap="paramBeanMap" statementType="CALLABLE"> { call proc_getUserAge( #{userId, mode=INOUT, jdbcType=INTEGER} ) } </select> </mapper>
(6)客户端代码如下:
package com.hxstrive.mybatis.procedure.demo3; 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/demo3/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); userMapper.getUserAge(paramBean); System.out.println("age=" + paramBean.getUserId()); } }
执行上面代码,执行成功后将输出用户年龄。
注意:
存储过程类型为 OUT 的参数必须使用 JavaBean 来接收,不能直接使用 Java 基础类型来接收,如:java.lang.String、Integer,笔者亲测过!!