无法从 mybatis 执行的 oracle 过程中接收参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7817185/
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
cannot receive out parameter from oracle procedure executed by mybatis
提问by Tural
I develop java application using Spring 3.0.5 and I work with database Oracle using mybatis-spring.
我使用 Spring 3.0.5 开发 java 应用程序,并使用 mybatis-spring 使用数据库 Oracle。
I've an interface for mybatis:
我有一个mybatis接口:
public interface SubscriberMapper {
Subscriber getSubscriberByMsisdn(String msisdn);
void insertSubscriber(Subscriber subscriber);
void updateSubscriber(Subscriber subscriber);
void canCustomerSubscribe(@Param("msisdn") String msisdn,
@Param("responseCode") Integer responseCode);
}
}
mybatis xml content for canCustomerSubscribe:
canCustomerSubscribe 的 mybatis xml 内容:
<parameterMap id="canCustomerSubscribeParams" type="map">
<parameter property="msisdn" jdbcType="VARCHAR" javaType="java.lang.String" mode="IN"/>
<parameter property="responseCode" jdbcType="NUMERIC" javaType="java.lang.Integer" mode="OUT"/>
</parameterMap>
<select id="canCustomerSubscribe" parameterMap="canCustomerSubscribeParams" statementType="CALLABLE">
CALL wallet.pkg_wallet_validation.can_customer_subscribe(#{msisdn}, #{responseCode})
</select>
and code to execute:
和要执行的代码:
public void subscribe(String msisdn) throws InvalidArgumentException {
Integer responseCode = 0;
subscriberMapper.canCustomerSubscribe(msisdn, responseCode);
System.out.println("msisdn: " + msisdn + ", responseCode: " + responseCode);
}
When I execute "subscribe" method with invalid "msisdn", I do not receive real out value from procedure. Execution of this procedure in the database returns reponseValue = 1001, but in Java code I receive 0. I turned on debug logging to stout for mybatis and output is:
当我使用无效的“msisdn”执行“subscribe”方法时,我没有从过程中收到真正的输出值。在数据库中执行此过程返回 reponseValue = 1001,但在 Java 代码中我收到 0。我打开调试日志记录到 mybatis 的 stout,输出是:
2011-10-19 10:32:46,732 DEBUG [main] (Slf4jImpl.java:28) ooo Connection Opened 2011-10-19 10:32:46,909 DEBUG [main] (Slf4jImpl.java:28) ==> Executing: CALL wallet.pkg_wallet_validation.can_customer_subscribe(?, ?) 2011-10-19 10:32:46,911 DEBUG [main] (Slf4jImpl.java:28) ==> Parameters: 509999999(String), 0(Integer) msisdn: 509999999, responseCode: 0
When I change in "subscribe" method responseCode = null, I receive and error:
当我更改“订阅”方法 responseCode = null 时,我收到并出错:
org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: Invalid column type ; uncategorized SQLException for SQL []; SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
回答by Tural
I found solution. Map must be user instead of two parameter in canCustomerSubscribe method.
我找到了解决方案。Map 必须是 user 而不是 canCustomerSubscribe 方法中的两个参数。
void canCustomerSubscribe(Map<String,Object> params);
mybatis xml content:
mybatis xml 内容:
<select id="canCustomerSubscribe" parameterType="java.util.HashMap" statementType="CALLABLE">
CALL wallet.pkg_wallet_validation.can_customer_subscribe(
#{msisdn, jdbcType=VARCHAR, javaType=java.lang.String, mode=IN},
#{responseCode,jdbcType=NUMERIC, javaType=java.lang.Integer, mode=OUT})
</select>
(I need to add the commas between arguments attributes)
(我需要在参数属性之间添加逗号)
calling it from subscribe service method:
从订阅服务方法调用它:
public void subscribe(String msisdn) throws InvalidArgumentException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("msisdn", msisdn);
params.put("responseCode", null);
subscriberMapper.canCustomerSubscribe(params);
System.out.println(params.get("responseCode"));
}