MyBatis Learning Summary (vi)--Call stored procedure (reprint)

Source: Internet
Author: User

Aloof, pale wolf

Only to find a way to succeed, not to find excuses for failure!

MyBatis Learning Summary (vi)--Call the stored procedure I. Request

  The number of males or females to be queried, if the incoming is 0 on the female otherwise is male

Ii. Preparing database tables and stored procedures
1CreateTableP_user (2 IDIntPrimaryKeyAuto_increment,3 Namevarchar10),4 SexChar2)5);67InsertInto P_user (name,sex)Values‘A‘, "male");8InsertInto P_user (name,sex)Values‘B‘, "female");9InsertInto P_user (name,sex)Values‘C‘, "male");1011--Create stored procedure (query to get the number of males or females, if the incoming is 0 female otherwise is male)12DELIMITER $13CREATEPROCEDURE Mybatis.ges_user_count (In sex_idINT, out User_countInt)14BEGIN15IF sex_id=0Then16SELECTCOUNT (*)From Mybatis.p_userWHERE P_user.sex=‘Woman‘IntoUser_count;17ELSE18SELECTCOUNT (*)From Mybatis.p_userWHERE P_user.sex=‘Man‘IntoUser_count;19ENDIF;20 end 21 $22 23 --< Span style= "COLOR: #008080" > call stored procedure 24 delimiter; 25 set  @user_count = 0;26 call Mybatis.ges_user_count (1,  @user_count 27 select  @user_count;   
Third, the editorUsermapper.xml

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

1<!--2The number of males or females to be queried, if the incoming is 0 on the female otherwise is male3-4<SelectId= "Getusercount"Parametermap= "Getusercountmap"StatementType= "Callable">5Call Mybatis.ges_user_count (?,?)6</Select>78<!--9Parametermap.put ("Sexid", 0);10Parametermap.put ("UserCount",-1);11-12<ParametermapType= "Java.util.Map"Id= "Getusercountmap">13<parameter property< Span style= "COLOR: #0000ff" >= "Sexid"  Mode= "in"  Jdbctype= "INTEGER" />14 <parameter property=" UserCount " Mode=" Out " Jdbctype=" INTEGER "/>15 </ parametermap>        
Iv. Writing Unit test code
1PackageMe.gacl.test;23ImportJava.util.HashMap;4ImportJava.util.List;5ImportJava.util.Map;67ImportMe.gacl.custom.model.ConditionUser;8ImportMe.gacl.domain.User;9ImportMe.gacl.util.MyBatisUtil;10ImportOrg.apache.ibatis.session.SqlSession;11ImportOrg.junit.Test;1213/**14*@authorGaCl15* Test Call stored Procedure16*/17PublicClassTest6 {1819@Test20PublicvoidTestgetusercount () {Sqlsession sqlsession =Mybatisutil.getsqlsession ();22/**23* Map the identity string of SQL,24* Me.gacl.mapping.userMapper is the value of the namespace attribute of the mapper tag in the Usermapper.xml file,25* Getusercount is the id attribute value of the select tag, and the value of the id attribute of the SELECT tag can be used to find the SQL to execute26*/String statement = "Me.gacl.mapping.userMapper.getUserCount";//Identity string for mapping sql28 map<string, integer> parametermap = new HashMap <string, Integer> ();  Parametermap.put ("Sexid", 1 Parametermap.put ("UserCount", -1  Sqlsession.selectone (statement, parametermap);  Integer result = Parametermap.get ("UserCount"   System.out.println (result)   Sqlsession.close ()  }36}       

MyBatis Learning Summary (vi)--Call stored procedure (reprint)

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.