IBatis (MyBatis): Working with Stored Procedures
Join the DZone community and get the full member experience.
Join For Freethis tutorial will walk you through how to setup ibatis ( mybatis ) in a simple java project and will present how to work with stored procedures using mysql.the goal os this tutorial is to demonstrate how to execute/call stored procedures using ibatis/ mybatis .
pre-requisites
for this tutorial i am using:
ide:
eclipse
(you can use your favorite one)
database:
mysql
libs/jars:
mybatis
,
mysql
conector and
junit
(for testing)
this is how your project should look like:
sample database
please run the script into your database before getting started with the project implementation. you will find the script (with dummy data) inside the sql folder.
as we are going to work with stored procedures, you will also have to execute a script with procedures. here are the procedures:
1 – spmapper – xml
i did not find anything on the user manual about how to call stored procedures, so i decided to search on the mailing list. and i found some tips of how to call stores procedures.
on the previous version, ibatis has a special xml tag for stored procedures. but there is no xml tag for it on current mybatis version (version 3).
to call a stored procedure usgin mybatis/ibatis 3 you will have to follow some tips:
- must set the statement type to callable
- must use the jdbc standard escape sequence for stored procedures: { call xxx (parm1, parm2) }
- must set the mode of all parameters ( in, out, inout )
- all in, out, and inout parameters must be a part of the parametertype or parametermap (discouraged). the only exception is if you are using a map as a parameter object. in that case you do not need to add out parameters to the map before calling , mybatis will add them for you automatically.
- resulttype or resultmap (more typically) is only used if the procedure returns a result set.
- important : oracle ref cursors are usually returned as parameters, not directly from the stored proc. so with ref cursors, resultmap and/or resulttype is usually not used.
first example:
we want to call the procedure gettotalcity and this procedure only have one out parameter, and no in/inout parameter. how to do it?
we are going to ser inline parameters in this first example. to use inline parameters, create a pojo class to represent your parameters, set the parametertype to the class you created and you are going to use this notation to represent each parameter:
#{parametername, mode=out, jdbctype=integer}
- mode can be in, out, inout
- and specify the jdbctype of your parameter
to create the mybatis xml configuration, you can use the select ou update tag. do not forget to set the statementtype to callable .
here is how our mybatis statement is going to look like:
and this is the pojo class which represents the parameter for gettotalcity procedure:
second example:
now we are going to try to call the same stored procedure we demonstrated on the first example, but we are going to use a parametermap, like you used to do in version 2.x.
a very important note: this is discouraged, please use inline parameters.
let’s declare the param pojo class as a parametermap:
and the stored procedure statment:
note that now we use “ ? ” (question mark) to represent each parameter.
third example:
now we are going to call a stored procedure with in and out parameters. let’s follow the same rules as the fisrt example.
we are going to use inline parameters and we are going to create a pojo class to represent our parameter.
mybatis code:
param2 pojo:
fourth example:
now let’s try to retrieve a resultset from the stored procedure. for this we are going to use a resultmap.
state pojo class:
2- spmapper – annotations
now let’s try to do the same thing we did using xml config.
annotation for first example (xml):
@select(value= "{ call gettotalcity( #{total, mode=out, jdbctype=integer} )}")@options(statementtype = statementtype.callable)object callgettotalcityannotations(param param);
it is very similiar to a simple select statement, but we have to set the statement type to callable. to do it, we can use the annotation @options .
with annotations, we can only use inline parameters, so we will not be able to represent the second exemple using annotations.
annotation for third example (xml):
the explanation is the same as first example, i am just going to list the code:
annotation for fourth example (xml):
i tried to set the fourth example with annotation, but the only thing i’ve got is this:
and it does not work. i tried to search on the mailing list, no luck. i could not find a way to represent a resultmap with annotation and stored procedures. i don’t know if it is a limitation. if you have any clue how to do it, please leave a comment, i will appreciate it!
download
if you want to download the complete sample project, you can get it from my github account: https://github.com/loiane/ibatis-stored-procedures
if you want to download the zip file of the project, just click on download:
there are more articles about ibatis to come. stay tooned!
from http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/
Opinions expressed by DZone contributors are their own.
Comments