【问题标题】:how to call stored function with mybatismybatis如何调用存储函数
【发布时间】:2019-01-21 07:20:25
【问题描述】:

我开始学习Mybatis,我搜索了如何处理存储函数。 我想知道如何用 mybatis 调用存储函数。 我可以使用这里描述的存储过程http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/

提前致谢。

【问题讨论】:

    标签: java mybatis


    【解决方案1】:

    你的映射器文件应该有这样的东西

    <update id="myMappedStatement" parameterType="map" statementType="CALLABLE">
      {#{returnedVal,javaType=String,jdbcType=VARCHAR,mode=OUT} = call myFunc(
           #{myParam1, javaType=String, jdbcType=VARCHAR,
           mode=IN},#{myParam2, javaType=String, jdbcType=VARCHAR,
           mode=IN},#{myParam3, javaType=String, jdbcType=VARCHAR,
           mode=IN})}   
    </update>
    

    调用函数应该如下所示:

    public String myFunction(Map myParams)
    {
      //assuming the dao has an Object sqlSessionFactory of type SqlSessionFactory
      SqlSession session = sqlSessionFactory.openSession();
      try
      {
        session.update("myMappedStatement",myParams);
        //now myParams contains an entry with key "returnedVal"
        return (String)myParams.get("returnedVal");   
      }
      catch (Exception ex)
      {
    
      }finally {
        session.close();
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以将返回值表示为 OUT 参数。

      { CALL #{retval, mode=OUT, jdbcType=INTEGER} = getResult(#{inval, mode=IN, jdbcType=INTEGER})}

      至少这是我在这里找到的: http://mybatis-user.963551.n3.nabble.com/How-to-map-function-call-td3457305.html

      【讨论】:

        【解决方案3】:

        我现在正在使用这个:

        <resultMap id="resultBalance" type="Balance">
            <result property="balance" column="BALANCE"/>
        </resultMap>
        
        <select id="getBalance" parameterType="Registration" resultMap="resultBalance">
            select MB_CHECK_BALANCE( #{account} , #{msisdn} ) as BALANCE from dual
        </select>
        

        【讨论】:

          【解决方案4】:

          您可以使用注释来做到这一点,如下所示:

          @Select(value = "select function(#{param1}, #{param2}) as returnedValueAlias")
          public ReturnedType getFunctionValue(
                              @Param("param1") Param1Type param1,
                              @Param("param2") Param2Type param2);
          

          【讨论】:

            【解决方案5】:

            这适用于ibatis,所以它也应该适用于mybatis:

            <parameterMap id="obtenerModoConsultaParams" class="java.util.HashMap" > 
                <parameter property="modoConsulta" jdbcType="INTEGER" javaType="java.lang.Integer"  mode="OUT"/>
            </parameterMap> 
            
            <procedure id="modoConsulta.element" parameterMap="obtenerModoConsultaParams" >
                {? = call proceso_consulta_ruc.modo_operacion_consulta_ruc ()} 
            </procedure>
            

            在 JAVA 中

            public Integer loadModoConsulta() throws Exception {
                Integer result = null;
                HashUtil<String, Object> param = new HashUtil<String, Object>();
                getSqlMapClientTemplate().queryForObject("modoConsulta.element", param);
                result = param.getInt("modoConsulta");
                return result;
            }   
            

            这对我有用。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-02-24
              • 2013-03-18
              • 1970-01-01
              • 1970-01-01
              • 2023-03-26
              • 2020-01-31
              • 2015-11-02
              • 1970-01-01
              相关资源
              最近更新 更多