oracle 用mybatis 3调用pl/sql函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16859123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling pl/sql function with mybatis 3
提问by Hernan Diaz
I have a function that returns a boolean value in pl/sql. I have tried to get directly that boolean value without success, so now I'm trying to convert it to string (I do not want to modify the database):
我有一个在 pl/sql 中返回布尔值的函数。我试图直接获取那个布尔值但没有成功,所以现在我试图将它转换为字符串(我不想修改数据库):
<parameterMap id="publicObject" type="map">
<parameter javaType="java.lang.Object" jdbcType="VARCHAR" mode="OUT" property="result" />
<parameter javaType="java.lang.String" jdbcType="VARCHAR" mode="IN" property="id" />
</parameterMap>
<select id="isPublicObject" parameterMap="publicObject" statementType="CALLABLE">
<![CDATA[
{
declare
v_bool BOOLEAN := TRUE;
begin
v_bool := PACKNAME.STF$IS_PUBLIC_OBJECT(#{id});
#{result} := CASE WHEN v_bool THEN 'TRUE' ELSE 'FALSE' END;
end;
}
]]>
</select>
Then I get this exception: "Error querying database. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #2 with JdbcType OTHER. Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: Invalid column type: 1111 ####
然后我得到这个异常:“查询数据库时出错。原因:org.apache.ibatis.type.TypeException:使用 JdbcType OTHER 为参数 #2 设置 null 时出错。尝试为此参数设置不同的 JdbcType 或不同的 jdbcTypeForNull 配置属性。原因:java.sql.SQLException:无效的列类型:1111 ####
This code works correctly in the database:
此代码在数据库中正常工作:
declare
v_bool BOOLEAN := TRUE;
v_str VARCHAR2(5);
begin
v_bool := PACKNAME.STF$IS_PUBLIC_OBJECT('000000');
v_str := CASE WHEN v_bool THEN 'TRUE' ELSE 'FALSE' END;
dbms_output.put_line('result:');
dbms_output.put_line(v_str);
end;
回答by Jarandinor
I wrote parameterType
& Map
example. It works on my test data.
我写了parameterType
&Map
例子。它适用于我的测试数据。
XML:
XML:
<update id="isPublicObject" parameterType="map" statementType="CALLABLE">
declare
v_bool BOOLEAN := TRUE;
begin
v_bool := PACKNAME.STF$IS_PUBLIC_OBJECT(#{id});
#{result,jdbcType=VARCHAR,mode=OUT} := CASE WHEN v_bool THEN 'TRUE' ELSE 'FALSE' END;
end;
</update>
Mapper:
映射器:
public interface PLSQLMapper {
public void isPublicObject(Map<String, Object> parameterMap);
}
Main:
主要的:
PLSQLMapper mapper = session.getMapper(PLSQLMapper.class);
Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("id", 1);
mapper.isPublicObject(parameterMap);
System.out.println("result: " + parameterMap.get("result"));
回答by Seeta Somagani
The #{} syntax can only be used for parameter substitution. It is not a variable that gets set after the execution of the PL/SQL. It can only be set before the prepared statement is built.
#{} 语法只能用于参数替换。它不是在执行 PL/SQL 后设置的变量。它只能在构建准备好的语句之前设置。
One way of going forward would be to use stored procedure and use an OUT param to get your result out.
前进的一种方法是使用存储过程并使用 OUT 参数来获取结果。