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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.