關於mybatis調用存儲過程獲取返回值問題

總體思路:map傳值

controller:

Map<String,Object> m=new HashMap<String,Object>();
        m.put("name", 'zs');
        m.put("password", '55555');
        cardservice.bindCard(m);
        JSONObject json=new JSONObject();
        //獲得返回值
        json.put("msg", m.get("msg"));
        json.put("result", m.get("result"));

service:

@Override
    public Object bindCard(Map<String,Object> m) {
        try {
            return cardMapper.bindCard(m);
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<String,Object>();
        }
    }

mapper:

Object bindCard(Map<String, Object> map) throws Exception;


<select id="bindCard" parameterType="map" statementType="CALLABLE" resultType="java.lang.Object">
    <![CDATA[  
    {call P_CardBindIdCardNo(
    #{name,mode=IN,jdbcType=VARCHAR},
    #{password,mode=IN,jdbcType=VARCHAR},
    #{result,mode=OUT,jdbcType=BIT},
    #{msg,mode=OUT,jdbcType=VARCHAR}
    )} 
  ]]>
</select>

20180522另一種方法:

mapper:

List<Map<String, Object>> getAnswerByModulesId(@Param("modulesId")int modulesId,@Param("patientCode")String patientCode) throws Exception;
    <select id="getAnswerByModulesId" resultType="map" statementType="CALLABLE" >
        EXEC getAnswerByModulesId #{modulesId},#{patientCode}
    </select

service

List<Map<String, Object>> getQuestionAnswerByModulesId(int modulesId,String patientCode);

@Override
    public List<Map<String, Object>> getQuestionAnswerByModulesId(int modulesId,String patientCode) {
        try {
            return naireMapper.getAnswerByModulesId(modulesId,patientCode);
        } catch (Exception e) {
            logger.error("getQuestionByModulesId異常!",e);
            return new ArrayList<Map<String,Object>>();
        }
    }

controller

@RequestMapping(value = "/getQuestionAnswerByModulesId", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getQuestionAnswerByModulesId(HttpServletRequest req,int modulesId) {
        JSONObject json = new JSONObject();
        try {
            @SuppressWarnings("unchecked")
            Map<String,Object> map=(Map<String,Object>)req.getSession().getAttribute("user");
            List<Map<String, Object>> list = naireService.getQuestionAnswerByModulesId(modulesId,(String)map.get("PatientCode"));
            json.put("questionList", list);
            json.put("result", true);
            json.put("msg", "獲取相應模塊成功!");
        } catch (Exception e) {
            json.put("result", false);
            json.put("msg", "獲取相應模塊失敗!");
            logger.error("getQuestionByModulesId異常!", e);
        }
        return json;
    }

20180810:

註意,切換數據源時,調用存儲過程時不能開啟事物,否則不能切換數據源

20181023

今天要獲得存儲過程的返回值,但不想用call方法感覺太麻煩,網上也沒找到資料,研究瞭一下可如此解決:

<select id="getUpdateHumanDisease" resultType="java.lang.String">
    declare @result varchar(50)
    exec updateHumanDisease #{0},#{1},#{2},@result output
    select @result
</select>

到此這篇關於mybatis調用存儲過程獲取返回值的文章就介紹到這瞭,更多相關mybatis獲取返回值內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: