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

喜你入骨 提交于 2019-12-12 19:25:11

问题


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}," + 
       ...

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" />
    ...

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?


回答1:


/* 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();



回答2:


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

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

and then pass the params to

public void callGetStatesAnnotations(params)


来源:https://stackoverflow.com/questions/34340628/mybatis-full-annotation-config-to-retrieve-stored-procedure-result-in-out-parame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!