Ext.: http://zachary-guo.iteye.com/blog/1756689
Mybats is a project renamed after IBatis was acquired by Google, and of course a lot of upgrades have been made. IBatis 2.x call stored procedure has a special label <procedure>, in Mybats 3.x there is no such tag, but by a parameter statementtype= "callable" to distinguish.
Stored procedures have three types of parameters, in (input parameters), out (output parameters), INOUT (input and output parameters). A stored procedure that can have multiple in parameters and at most one out or INOUT parameter.
◇ stored procedure with only in Parameters
SQL code
- CREATE PROCEDURE proc_only_input (
- @hello VARCHAR (8) in
- ) as
- ...
XML code
- <select id="selectsth" statementtype="callable" parametertype= "hashmap">
- <! [Cdata[
- {Call Proc_only_input (#{good, Mode=in, Jdbctype=varchar})}
- ]]>
- </Select>
Java code
- MAP params = new HashMap ();
- The parameter names passed by the calling stored procedure are not consistent with the parameter names that define stored procedures, as long as they are guaranteed to be in the same order.
- Params.put ("good", "China");
- Session.select ("Pkg.selectsth", params);
◇ stored procedure with INOUT or out parameters
SQL code
- CREATE PROCEDURE proc_out (
- @yes VARCHAR (8) in,
- @fly VARCHAR (+) out
- ) as
- ...
- Return ' return something '
XML code
- <!--
- If an out parameter is present in the calling procedure, all arguments must be passed in as a question mark, such as: {call test_procedure (?,?,?,?)}. If a parameter is not passed in as a question mark, such as: {Call Test_procedure (?, 3,?,?)}, it will be unhandled: Output parameter not allowed as argument list prevents use of RP C
- In addition, for stored procedures with output parameters, it is theoretically possible to write:
- {#{gog, mode=out, jdbctype=varchar} = Call Proc_out (#{yes, mode= in, jdbctype=VARCHAR} ) }
- But I try to write in SQL Server, error, say is @fly parameter (Proc_out's second parameter name is fly). In that case, I'll write a parameter, just like this one.
- -->
- <select id="selectsth" statementtype="callable" parametertype= "hashmap">
- <! [Cdata[
- {Call Proc_out (#{yes, Mode=in, Jdbctype=varchar}, #{gog, Mode=out, Jdbctype=varchar})}
- ]]>
- </Select>
Java code
- MAP params = new HashMap ();
- The parameter names passed by the calling stored procedure are not consistent with the parameter names that define stored procedures, as long as they are guaranteed to be in the same order.
- Also, the value of the output parameter of the stored procedure must be received through the map
- Params.put ("yes", "China");
- Session.select ("Pkg.selectsth", params);
- Get the value of the output parameter
- String result = params. (String) Get ("Gog");
Using stored procedures in MyBatis 3