Skip to main content

Java MyBatis stored procedure call with OUT parameters



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





Database: SQL Server 2008





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!





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







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;

}

}







Thanks!


Comments

  1. 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, 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.

    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>


    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?

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex