• 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"));
    View Code

    service:

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

    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>
    View Code

    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>
    View Code

    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>>();
            }
        }
        
    View Code

    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;
        }
    View Code

    20180810:

    注意,切换数据源时,调用存储过程时不能开启事物,否则不能切换数据源

    20181023

    今天要获得存储过程的返回值,但不想用call方法感觉太麻烦,网上也没找到资料,研究了一下可如此解决:

    <select id="getUpdateHumanDisease" resultType="java.lang.String">
        declare @result varchar(50)
        exec updateHumanDisease #{0},#{1},#{2},@result output
        select @result
    </select>
  • 相关阅读:
    一些常用的 Git 进阶知识与技巧
    自动充值平台开发进程之联通卡密一最终实现
    自动充值平台开发进程之联通卡密一准备阶段
    自动充值平台开发进程之联通卡密一显示界面
    [注入] 突破 SESSION 0 隔离的远线程注入
    git 撤销 push 提交
    [C#] 获取时间戳(秒/毫秒)
    html判断浏览器是否支持,不支持则输出
    [C#] MD5 加密
    [Windows驱动开发] 进程的pid handle eprocess之间相互转换的方法
  • 原文地址:https://www.cnblogs.com/yanan7890/p/8776379.html
Copyright © 2020-2023  润新知