MyBatis完整注释配置来检索OUT参数中的存储过程结果? [英] MyBatis full annotation config to retrieve stored procedure result in OUT parameter?

查看:323
本文介绍了MyBatis完整注释配置来检索OUT参数中的存储过程结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Oracle存储过程,该存储过程以SYS_REFCURSOR类型的OUT参数返回其结果.我们想通过MyBatis映射器来调用它,@ Select批注中查询字符串的相关部分如下所示

We have an Oracle stored procedure that returns its results in a SYS_REFCURSOR type OUT parameter. We would like to call this through a MyBatis mapper, the relevant part of the query string in the @Select annotation looks as follows

@Select(value="call " + SCHEMA_NAME + "." + STORED_PROCEDURE_NAME +
      "(" + ...
      "#{" + P_RECORDSET_FIELD + ",javaType=java.sql.ResultSet,jdbcType=CURSOR,mode=OUT,resultMap=ownNameSpace.ownResultMap}," + 
       ...

其中resultMap属性引用以下XML配置

where the resultMap property refers to the following XML configuration

<mapper namespace="ownNameSpace">
  <resultMap id="ownResultMap" type="com.ownpackage.OwnResultType">
    <result column="COLUMN_1" property="property1" />
    ...

这很好用,DAO类使用映射器成功地从数据库中检索了预期的结果.但是,我们想知道是否可以仅使用注释而无需XML来解决此问题. MyBatis具有@ Results/@ Result/@ ResultMap批注,我们已成功将其用于带有ResultSet的SP,但到目前为止,我们还无法真正找到OUT参数的解决方案.类似的示例通常归结为使用混合注解+ XML配置.例如.以下教程的作者似乎已经遇到了同样的问题,尽管它已经存在了几年了: https://dzone.com/articles/ibatis-mybatis-working-stored (请参阅第四个示例的注释),这完全可行吗?

This works perfectly, the expected results are succesfully retrieved from the DB by the DAO class using the mapper. However we wonder whether it is possible to solve this without XML, using annotations only. MyBatis has @Results/@Result/@ResultMap annotation which we succesfully use for SPs with ResultSet but so far we could not really find a solution for OUT parameters. Similar examples usually boil down to using a mixed annotations+XML configuration. E.g. the author of the following tutorial seems to stuck with the same issue, though it is a few years old: https://dzone.com/articles/ibatis-mybatis-working-stored (see Annotation for Fourth Example) Is this feasible at all?

推荐答案

尝试这样..

@Select(value = "{ CALL getTotalCityStateId(" +
        "#(stateCursor, mode=OUT, jdbcType=CURSOR," +
        "javaType=java.sql.ResultSet,resultMap = StageCursorMap } )}")
@Options(statementType = StatementType.CALLABLE)
@ResultType(State.class)
@Results(id = "StageCursorMap",
        value = {
                @Result(property = "id", column = "state_id"),
                @Result(property = "name", column = "state_name"),
                @Result(property = "code", column = "state_code"),
        })
public void callGetStatesAnnotations(State state);

如果必须传递IN参数,请使用

if you have to pass the IN parameter, use

Map<String, Object> params = new HashMap<String, Object>();

然后将参数传递给

public void callGetStatesAnnotations(params)

这篇关于MyBatis完整注释配置来检索OUT参数中的存储过程结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆