接口
public interface TestMapper {
void execuStoredprocedure(Map<String, String> paramMap);
}
- 通过
statementType="CALLABLE"
指定存储过程调用 - 通过
mode=IN
指定输入参数
- 通过
mode=OUT
指定输出参数
<?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="XXX.TestMapper">
<select id="execuStoredprocedure" parameterType="java.util.Map" statementType="CALLABLE">
{CALL
存储过程名称(
#{param1,mode=IN,jdbcType=VARCHAR},
#{param2,mode=IN,jdbcType=VARCHAR},
#{param3,mode=OUT,jdbcType=INTEGER})
}
</select>
</mapper>
如果调用存储过程,既有出参,又有返回值的话
<?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="XXX.TestMapper">
<select id="execuStoredprocedure" parameterType="java.util.Map" statementType="CALLABLE">
{
<!-- ⏹在CALL之前添加如下代码即可⏹ -->
#{param4, mode=OUT, jdbcType=VARCHAR} = CALL 存储过程名称(
#{param1, mode=IN, jdbcType=VARCHAR},
#{param2, mode=IN, jdbcType=VARCHAR},
#{param3, mode=OUT, jdbcType=INTEGER})
}
</select>
</mapper>
调用
Map<String, Object> paramMap = new HashMap<String, Object>();
// 输入参数,向存储过程中传值
map.put("param1", "测试参数1");
map.put("param2", "测试参数2");
// 输出参数,存储过程的返回值会保存到该key对应的value中
map.put("param3", null);
// 用来接收存储过程的返回值(若存在返回值的话)
map.put("param4", null);
/*
虽然我们接口无返回值,但是因为调用的是存储过程,并且指定了param3为输出参数
因此存储过程执行的结果会反映到Map中,调用完存储过程之后,可以在map中获取到返回值
*/
mapper.execuStoredprocedure(paramMap);
// 调用完存储过程之后,获取返回值
System.out.println(paramMap.get("param3"));
参考资料
- https://blog.csdn.net/chujianbi7142/article/details/100894651
- https://www.cnblogs.com/Marydon20170307/p/14134373.html
- https://blog.csdn.net/dwenxue/article/details/82257944