带有OUT参数的Java MyBatis存储过程调用

时间:2012-02-09 17:32:06

标签: java stored-procedures mybatis out-parameters

第一个问题: 我试图返回一个OUT参数而不是带注释的结果集。首先,它甚至可能吗?如果是,那怎么会这样做?

MyBatis:3.0.6

数据库:SQL Server 2008

以下是UserDAO中我的方法调用的语法示例:

@Select(value= "{ CALL saveUser( "
        + "#{userId, mode=IN, jdbcType=INTEGER},"
        + "#{firstname, mode=IN, jdbcType=VARCHAR},"
        + "#{lastname, mode=IN, jdbcType=VARCHAR},"
        + "#{message, mode=OUT, jdbcType=VARCHAR}"
        + ")}")
@Options(statementType=StatementType.CALLABLE)
public String saveUser(
        @Param("userId") int userId,
        @Param("firstname") String firstname,
        @Param("lastname") String lastname);

我从所有“保存”程序返回一条消息,因此我可以向用户返回响应:“用户保存成功”,“保存用户时出错”,“您无权保存此用户”等我知道返回结果集会解决问题,只是我不想改变我的所有程序!

第二个问题:是否可以返回从多个OUT参数填充的“SaveProcedureResponse”?例如:

@Select(value= "{ CALL saveUser( "
        + "#{userId, mode=IN, jdbcType=INTEGER},"
        + "#{firstname, mode=IN, jdbcType=VARCHAR},"
        + "#{lastname, mode=IN, jdbcType=VARCHAR},"
        + "#{message, mode=OUT, jdbcType=VARCHAR},"
        + "#{status, mode=OUT, jdbcType=VARCHAR},"
        + "#{returnCode, mode=OUT, jdbcType=INTEGER}"
        + ")}")
@Options(statementType=StatementType.CALLABLE)
public SaveProcedureResponse saveUser(
        @Param("userId") int userId,
        @Param("firstname") String firstname,
        @Param("lastname") String lastname);

bean看起来像这样:

public class SaveProcedureResponse {
    private String message;
    private String status;
    private int returnCode;

    public SaveProcedureResponse(String message, String status, int returnCode) {
        this.message = message;
        this.status = status;
        this.returnCode = returnCode;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:7)

  

第一个问题:我正在尝试返回一个OUT参数而不是一个   带注释的结果集。首先,它甚至可能吗?如果是,如何   会这样做吗?

错误,有点儿。 Mapper不会return输出参数,但您可以让Mybatis将它们设置到参数对象上,或将它们放入像this这样的地图中。

因此给出了一个简单的java对象,其中包含所有字段的getter和setter。在调用映射器之后,out参数将被设置到对象上。

<update id="someProcedure" statementType="CALLABLE">
    {call some procedure(
            #{someInParamA, mode=IN},
            #{someInParamB, jdbcType=ARRAY, mode=IN},
            #{someOutParamA, javaType=Boolean, jdbcType=NUMERIC, mode=OUT },
            #{someOutParamB, javaType=Object, jdbcType=ARRAY, jdbcTypeName=SOMEJDBCTYPE, mode=OUT})}
</update>

因此,要获得out参数,它看起来就像这样。

mapper.getSomeProcedure(someBean);
//out params populated on the object passed to the mapper :)
String outA = bean.getSomeOutParamA();

这有点难以解释,这有意义吗?

答案 1 :(得分:5)

使用MyBatis-Spring注释,编写这样的代码。

public interface ProductMapper
{
    @Select("exec Pup_ProductSearch_Sel #{searchString}, #{pageNum}, #{pageSize}, #{totalRows,mode=OUT,jdbcType=NUMERIC}")
    @Options(statementType = StatementType.CALLABLE)
    public List<Map<String, Object>> productSearch(ProductSearchParameters params);


    public class ProductSearchParameters
    {
        private String searchString = null;
        private Integer pageNum = 1;
        private Integer pageSize = 5;
        private Integer totalRows = -1;

        // Accessors go here...
    }
}

IN参数由我们在调用代码中填充。 OUT参数由数据访问层填充,并在调用映射器后出现在params对象中。

@Service
public class TestService
{
    @Resource
    private ProductMapper mapper;


    public void run()
    {
        final ProductMapper.ProductSearchParameters params = new ProductMapper.ProductSearchParameters();
        params.setSearchString("book"); // IN parameter
        final List<Map<String, Object>> results = mapper.productSearch(params);

        for(final Map<String, Object> product : results)
        {
            System.out.println(product.get("title"));
        }

        System.out.println("Total results: " + params.getTotalRows()); // OUT parameter
    }
}

受到这篇文章的启发:http://ibatis.10938.n7.nabble.com/IBatis-3-0-beta-10-annotations-stored-procedures-td7806.html