繁体   English   中英

如何使用mybatis调用存储的函数

[英]how to call stored function with mybatis

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

提前致谢。

您的映射器文件应具有以下内容

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

我现在正在使用这个:

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

您可以使用注释来执行此操作,如下所示:

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

您可以将返回值表示为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

这适用于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;
}   

这对我行得通。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM