MyBatis full annotation config to retrieve stored

2019-07-13 13:07发布

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?

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-13 13:19

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)
查看更多
萌系小妹纸
3楼-- · 2019-07-13 13:33

/* 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();
查看更多
登录 后发表回答