存储过程调用

一、jdbcTemplate 调用方式
public AjaxResult doSettlement(@RequestParam String userName, @RequestParam String userPhone, @RequestParam String relationId)
    {

        if (StringUtils.isAnyBlank(userName) || StringUtils.isAnyBlank(userPhone) || StringUtils.isAnyBlank(relationId)) {
            return AjaxResult.error("参数不能为空", false);
        }

    	// 调用存储过程
        int o = (Integer)jdbcTemplate.execute(connection -> {
            String storedProc = "{CALL YB_BILL_ACCOUNT(?,?,?,?)}";// 调用的sql
            CallableStatement cs = connection.prepareCall(storedProc);
            cs.setString(1, relationId);// 设置输入参数的值
            cs.setString(2, userName);// 设置输入参数的值
            cs.setString(3, userPhone);// 设置输入参数的值
            cs.registerOutParameter(4, Types.INTEGER);// 注册输出参数的类型
            return cs;
        }, (CallableStatementCallback) callableStatement -> {
            callableStatement.execute();
            return callableStatement.getInt(4);
        });

        if (o == 0) {
            return AjaxResult.error("结算失败", false);
        }

        return AjaxResult.success("结算成功", true);
    }
二、Mybatis 调用方式

2.1 mapper接口

public interface TestMapper {

    void execuStoredprocedure(Map<String, String> paramMap);
}

2.2 mapper的xml文件

  • 通过statementType="CALLABLE"指定存储过程调用
  • 通过mode=IN指定入参
  • 通过mode=OUT指定返回值,用来接受存储过程的返参(这里我们可以看到 param3 被指定为返回值)
<?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>

2.3 impl实现类调用

//将所需参数装入map
Map<String, Object> paramMap = new HashMap<String, Object>();
map.put("param1", "测试参数1");
map.put("param2", "测试参数2");
map.put("param3", null);

//执行调用
mapper.execuStoredprocedure(paramMap);


//调用完存储过程之后,获取返回值(在第二步中已将 param3 指定为接受返回值)
System.out.println(paramMap.get("param3"));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值