Using stored procedures in MyBatis 3

Source: Internet
Author: User
Tags cdata

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
    1. CREATE PROCEDURE proc_only_input (
    2. @hello VARCHAR (8) in
    3. ) as
    4. ...

XML code
    1. <select id="selectsth" statementtype="callable" parametertype= "hashmap">
    2. <! [Cdata[
    3. {Call Proc_only_input (#{good, Mode=in, Jdbctype=varchar})}
    4. ]]>
    5. </Select>

Java code
    1. MAP params = new HashMap ();
    2. 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.
    3. Params.put ("good", "China");
    4. Session.select ("Pkg.selectsth", params);


◇ stored procedure with INOUT or out parameters

SQL code
    1. CREATE PROCEDURE proc_out (
    2. @yes VARCHAR (8) in,
    3. @fly VARCHAR (+) out
    4. ) as
    5. ...
    6. Return ' return something '

XML code
  1. <!--
  2. 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
  3. In addition, for stored procedures with output parameters, it is theoretically possible to write:
  4. {#{gog, mode=out, jdbctype=varchar} = Call Proc_out (#{yes, mode= in, jdbctype=VARCHAR} ) }  
  5. 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.
  6. -->
  7. <select id="selectsth" statementtype="callable" parametertype= "hashmap">
  8. <! [Cdata[
  9. {Call Proc_out (#{yes, Mode=in, Jdbctype=varchar}, #{gog, Mode=out, Jdbctype=varchar})}
  10. ]]>
  11. </Select>

Java code
      1. MAP params = new HashMap ();
      2. 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.
      3. Also, the value of the output parameter of the stored procedure must be received through the map
      4. Params.put ("yes", "China");
      5. Session.select ("Pkg.selectsth", params);
      6. Get the value of the output parameter
      7. String result = params.  (String) Get ("Gog");

Using stored procedures in MyBatis 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.