简体   繁体   English

MyBatis完整注释配置来检索OUT参数中的存储过程结果?

[英]MyBatis full annotation config to retrieve stored procedure result in OUT parameter?

We have an Oracle stored procedure that returns its results in a SYS_REFCURSOR type OUT parameter. 我们有一个Oracle存储过程,该存储过程以SYS_REFCURSOR类型OUT参数返回其结果。 We would like to call this through a MyBatis mapper, the relevant part of the query string in the @Select annotation looks as follows 我们想通过MyBatis映射器调用它,@ Select批注中查询字符串的相关部分如下所示

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

where the resultMap property refers to the following XML configuration 其中resultMap属性引用以下XML配置

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

This works perfectly, the expected results are succesfully retrieved from the DB by the DAO class using the mapper. 这非常完美,DAO类使用映射器成功地从数据库中检索了预期的结果。 However we wonder whether it is possible to solve this without XML, using annotations only. 但是,我们想知道是否可以仅使用注释而无需XML来解决此问题。 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. MyBatis具有@ Results / @ Result / @ ResultMap批注,我们已成功将其用于带有ResultSet的SP,但到目前为止,我们还无法真正找到OUT参数的解决方案。 Similar examples usually boil down to using a mixed annotations+XML configuration. 类似的示例通常归结为使用混合注解+ XML配置。 Eg 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? 例如,以下教程的作者似乎已经遇到了同样的问题,尽管它已经使用了几年了: https : //dzone.com/articles/ibatis-mybatis-working-stored (请参见第四个示例的注释)这可行吗?完全没有?

/* All you need to do is declare you result type see below example*/ / *您所需要做的就是声明您的结果类型,请参见下面的示例* /

@Select(value= "{ CALL getTotalCityStateId()}")
@Options(statementType = StatementType.CALLABLE)
@ResultType(State.class)
@Results(
{   
 @Result(property="id", column="state_id"),
 @Result(property="name", column="state_name"),
 @Result(property="code", column="state_code"),
})
List<State> callGetStatesAnnotations();

Try like this.. 像这样尝试

@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);

if you have to pass the IN parameter, use 如果必须传递IN参数,请使用

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

and then pass the params to 然后将参数传递给

public void callGetStatesAnnotations(params)

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

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