前不久接到一个需求,其中部分业务需要调用存储过程来完成,在查看了一些别人的写法之后,自己也来分享一下自己的写法:
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();
}
}