我开始学习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>
Run Code Online (Sandbox Code Playgroud)
调用函数应如下所示:
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();
}
}
Run Code Online (Sandbox Code Playgroud)