java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis获取返回值

关于mybatis调用存储过程获取返回值问题

作者:每天都要学一点

这篇文章主要介绍了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获取返回值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文