MyBatis调用存储过程

这篇博客分享了如何在MyBatis中调用存储过程。内容包括从controller到service再到DAO和Mapper.xml的详细步骤,虽然存储过程有返回值,但因业务需求并未在文档中展示获取返回值的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        前不久接到一个需求,其中部分业务需要调用存储过程来完成,在查看了一些别人的写法之后,自己也来分享一下自己的写法:
Begin:
1.controller:

	@RequestMapping(value = "/save",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public MgtReportResp<Object> addAppear(@RequestBody WyAppearRequest pmRequest,
                                            HttpServletRequest request) {
        RespBuilder<Object> builder = new RespBuilder<Object>();
        try{
             wyAppearService.storeCall(StatusCode.DEAL_ROLLBACK, pmRequest);  
        }catch (Exception e){
            logger.debug("数据退回失败!");
            return builder.buildResp(ResultCode.FAIL, "上报数据调整中出现问题,请联系管理员处理!!!");
        }
        return builder.buildResp(ResultCode.SUCCESS, "操作成功");
    }

2.service:

/**
     * 调用存储过程
     * @param dealType
     * @param pmRequest
     * @return
     */
    void storeCall(int dealType,WyAppearRequest pmRequest);
@Override
    public void storeCall(int dealType,WyAppearRequest pmRequest) {
        try {
            StoreFunction store = new StoreFunction();
            store.setiCompanyId(pmRequest.getCompanylist().get(0));
            store.setiAccountYear(pmRequest.getYear());
            store.setiAccountMonth(pmRequest.getMonthlist());
            store.setiDealType(dealType);
            storeFunctionDao.storeCall(store);
        }catch (Exception e){
            logger.debug("存储过程调用失败!",e);
            e.printStackTrace();
        }
    }

3.DAO:

 /**
     * 存储过程的调用
     * @param storeFunction
     * @return
     */
    void storeCall (StoreFunction storeFunction);

4.Mapper.xml

<!-- 存储过程函数的调用 -->
    <select id="storeCall" statementType="CALLABLE" parameterType="java.util.Map" >
        {
            #{result,mode=OUT,jdbcType=INTEGER} = call BD_WY_XMTJ_GWHX.FUNC_WY_GWHX_COSTS_ADJUST(
                #{iDealType,mode=IN,jdbcType=INTEGER},
                #{iAccountYear,mode=IN,jdbcType=VARCHAR},
                #{iAccountMonth,mode=IN,jdbcType=VARCHAR},
                #{iCompanyId,mode=IN,jdbcType=VARCHAR}
            )
		}
    </select>

END。

注:
        由于存储过程不是本人自己所写,故在此文档中就不贴出来了,同时该存储过程是有返回值,但是业务不需要返回值的,所以文档中也没有进行返回值的获取。

在此处写一下又返回值时service的 处理:

@Override
    public int storeCall(int dealType,WyAppearRequest pmRequest) {
        StoreFunction store = new StoreFunction();
        try {
            store.setiCompanyId(pmRequest.getCompanylist().get(0));
            store.setiAccountYear(pmRequest.getYear());
            store.setiAccountMonth(pmRequest.getMonthlist());
            store.setiDealType(dealType);
            storeFunctionDao.storeCall(store);
        }catch (Exception e){
            logger.debug("存储过程调用失败!",e);
            e.printStackTrace();
        }
        if(null != store.getResult()){
            return 0;
        }else{
            return store.getResult().intValue();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值