带有OUT参数的Java MyBatis存储过程调用 [英] Java MyBatis stored procedure call with OUT parameters

查看:866
本文介绍了带有OUT参数的Java MyBatis存储过程调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题: 我正在尝试返回一个OUT参数,而不是带有注释的结果集.首先,有可能吗?如果是这样,怎么办?

First question: I am trying to return one OUT parameter and not a result set with annotations. First, is it even possible? If it is, how would one do this?

MyBatis:3.0.6

MyBatis: 3.0.6

数据库:SQL Server 2008

Database: SQL Server 2008

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

Here is an example of the syntax of my method call in the 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);

我正在从所有保存"过程中返回一条消息,因此我可以向用户返回响应:用户保存成功",用户保存错误",您无权保存该用户"等等.我知道返回结果集将解决问题,只是我不想更改所有过程!

I am returning a message from all of my "save" procedures and so I can return a response to the user: "User save successfully","Error saving user","You do not have permission to save this user", etc. I know that returning a result set will solve the problem, its just that I don't want to change all of my procedures!

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

Second question: Is it possible to return a "SaveProcedureResponse" populated from multiple OUT parameters? For example:

@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看起来像这样:

Where the bean looks like this:

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;
    }
}

谢谢!

推荐答案

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

First question: I am trying to return one OUT parameter and not a result set with annotations. First, is it even possible? If it is, how would one do this?

err,有点.映射器不会return out参数,但是您可以让Mybatis将其设置到参数对象上,或将它们放入

err, sorta. The Mapper won't return the out parameters, but you can get Mybatis to set them onto the parameter object, or put them into a map like this.

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

So given a simple java object with getters and setters for all the fields. After a call to the mapper, the out parameters will be set onto the object.

<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参数,它看起来像这样.

So to get the out parameters, it would look something like this.

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

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

It's kinda hard to explain, does that make sense?

这篇关于带有OUT参数的Java MyBatis存储过程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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