mybatis 调用存储过程
在 JDBC 中调用已储存过程的语法如下所示。注意,方括号表示其间的内容是可选项;方括号本身并不是语法的组成部份。
{call 过程名[(?, ?, ...)]}
返回结果参数的过程的语法为:
{? = call 过程名[(?, ?, ...)]}
不带参数的已储存过程的语法类似:
{call 过程名}
带返回值XML配置
<select id="getReturnCount" parameterType="java.util.Map" statementType="CALLABLE"> <![CDATA[ {#{cityCount,mode=OUT,jdbcType=INTEGER}=call dbo.p_logic_test_web( #{cityId,mode=IN,jdbcType=INTEGER} )} ]]> </select>
返回结果为Map,直接从map中的key=cityCount取值
非XML配置
/** * 执行预分存储过程 * * @param allocateDate:预分日期,如:2017-01-01 00:00:00 * @param planMoney:计划分成金额:单位为分 * @throws Exception * @return:实际分成金额 */ public void execPreAllocateProcedure(String allocateDate, int planMoney, Integer linkId) throws Exception { String call = "{call p_logic_shortcut_preallocate_money_day_web(?,?,?)}"; CallableStatement callableStatement=null; Connection conn = getDataSource().getConnection(); java.sql.Date date = new java.sql.Date(DateUtil.convertStringToDate(allocateDate, "yyyy-MM-dd HH:mm:ss").getTime()); try { callableStatement = conn.prepareCall(call); callableStatement.setDate(1, date); callableStatement.setInt(2, planMoney); callableStatement.setInt(3, linkId); callableStatement.execute(); } catch (Exception e) { throw new IspRollBackException("调用存储过程出错:" + e.getMessage()); } finally { callableStatement.close(); conn.close(); } }