简体   繁体   English

Mybatis在执行存储过程时无法设置OUT参数

[英]Mybatis could not set OUT parameter when executing stored procedure

I am following this question to call a stored procedure with 1 input parameter and 1 out parameter:我正在关注这个问题以调用具有 1 个输入参数和 1 个输出参数的存储过程:

Mapper:映射器:

package myapp.test.persistence;

import myapp.test.ExpectationProcessMessage;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ExpectationProcessMessageMapper {
    List<ExpectationProcessMessage> processExpectation(Map<String, Object> params);
}

XML content: XML 内容:

<mapper namespace="myapp.test.persistence.ExpectationProcessMessageMapper">

    <resultMap id="expectationProcessMessages" type="myapp.test.ExpectationProcessMessage">
        <id property="errorCode" column="error_code"/>
        <result property="errorMessage" column="error_message"/>
    </resultMap>

    <select id="processExpectation" parameterType="java.util.HashMap" resultMap="expectationProcessMessages">
      {call cp_process_expectation_api (#{ai_expectation_id,jdbcType=NUMERIC, javaType=java.lang.Integer,mode=IN},
                                         #{ac_process_flag,jdbcType=VARCHAR, javaType=java.lang.String,mode=OUT})}
    </select>
</mapper>

Call class:调用类:

package myapp.test.persistence;

@Repository
public class SqlExpectationRepositoryImpl implements ExpectationRepository {
    ...
    @Override
    public ExpectationProcess processExpectation(Integer expectationId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ai_expectation_id", expectationId);
        params.put("ac_process_flag", null);
        List<ExpectationProcessMessage> expectationProcessMessages = expectationProcessMessageMapper.processExpectation(params);
        System.out.println(params.get("ac_process_flag"));
    }
    ...
}

Stored procedure definition:存储过程定义:

begin
    declare @ai_expectation_id int = 0
    declare @ac_process_flag char
    declare @result int
    exec
        @result = cp_process_expectation_api
            @ai_expectation_id,
            @ac_process_flag output

    select @ac_process_flag

    select @result as result
end

But I got an exception:但我有一个例外:

Cause: com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 2.原因:com.microsoft.sqlserver.jdbc.SQLServerException:未为参数编号 2 设置值。
Uncategorized SQLException;未分类的 SQLException; SQL state [null]; SQL 状态 [null]; error code [0];错误代码 [0]; The value is not set for the parameter number 2.;未为参数编号 2 设置该值。 nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 2.嵌套异常为 com.microsoft.sqlserver.jdbc.SQLServerException:未为参数编号 2 设置值。

I have found the solution.我找到了解决办法。 We need to add statementType="CALLABLE" property to XML file.我们需要在 XML 文件中添加 statementType="CALLABLE" 属性。 It will be like this:它会是这样的:

<mapper namespace="com.gbst.processmanagement.infra.persistence.expectation.expectation.ExpectationProcessMessageMapper">

    <resultMap id="expectationProcessMessages" type="com.gbst.processmanagement.domain.expectation.expectation.ExpectationProcessMessage">
        <id property="errorCode" column="error_code"/>
        <id property="messageType" column="message_type"/>
        <result property="errorPointer" column="error_pointer"/>
        <result property="errorMessage" column="error_message"/>
    </resultMap>

    <select id="processExpectation" statementType="CALLABLE" parameterType="java.util.HashMap" resultMap="expectationProcessMessages">
      {call cp_process_expectation_api (#{expectationId,jdbcType=NUMERIC, javaType=java.lang.Integer,mode=IN},
                                        #{processFlag,jdbcType=VARCHAR, javaType=java.lang.String,mode=OUT})}
    </select>
</mapper>


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

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