MyBatis-Invoke the stored procedure

Source: Internet
Author: User

I. Presenting the demand

Query gets the number of males or females, if the incoming is 0 on the female otherwise is male two, prepare database tables and stored procedures

 1 CREATE TABLE P_user (2 ID int primary key auto_increment, 3 name varchar (ten), 4 sex char (2) 5);  
 6 7 INSERT INTO P_user (name,sex) VALUES (' A ', "male");  
 8 INSERT INTO P_user (name,sex) VALUES (' B ', "female");  
9 INSERT INTO P_user (name,sex) VALUES (' C ', "male"); 10 11-Create stored procedure (query gets number of males or females, if incoming is 0 female otherwise male) DELIMITER $ CREATE PROCEDURE mybatis.ges_user_count (in sex_id INT, O UT user_count INT) sex_id=0 THEN SELECT Count (*) from Mybatis.p_user WHERE p_user.sex= ' female ' into User_co
Unt
/ELSE SELECT COUNT (*) from Mybatis.p_user WHERE p_user.sex= ' man ' into user_count;
End IF;
End 21 $22 23--Call stored procedure DELIMITER;
SET @user_count = 0;
Call Mybatis.ges_user_count (1, @user_count); SELECT @user_count; 
Third, edit Usermapper.xml

Edit the Usermapper.xml file and add the following configuration items

1 <!-- 
 2         Inquiries get the number of males or females, if the incoming is 0 for females otherwise it is male
 3      -->
 4     <select id= "Getusercount" parametermap= "Getusercountmap" statementtype= "callable" >
 5 Call         mybatis.ges_user_count (?,?)
 6     </select>
 7     
 8     <!--
 9         parametermap.put ("Sexid", 0);         parametermap.put ("UserCount",-1);
One      -->     <parametermap type= "Java.util.Map" id= "Getusercountmap" >         < Parameter property= "Sexid" mode= "in" jdbctype= "INTEGER"/> <parameter property= "UserCount" mode= "         Out "jdbctype="/>     </parameterMap>

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.