MyBatis Learning Tutorial (vi)-calling stored procedures _java

Source: Internet
Author: User

I. Presenting the demand

Query gets the number of males or females, if the incoming is 0 for females otherwise it is male

Ii. preparing database tables and stored procedures

CREATE TABLE P_user ( 
ID int primary key auto_increment, 
name varchar (),
sex char ()
); 
Insert into P_user (name,sex) VALUES (' A ', "male"); 
Insert into P_user (name,sex) VALUES (' B ', ' female '); 
Insert into P_user (name,sex) VALUES (' C ', ' Male '); 
--Create a stored procedure (a query gets the number of males or females, if the female is male otherwise)
DELIMITER $
CREATE PROCEDURE mybatis.ges_user_count (in sex_id INT, Out User_count INT)
BEGIN 
IF sex_id= THEN
SELECT Count (*) from Mybatis.p_user WHERE p_user.sex= ' female ' into US Er_count;
ELSE
SELECT COUNT (*) from Mybatis.p_user WHERE p_user.sex= ' man ' into User_count;
End IF;
End 
$
-Call stored procedure
DELIMITER;
SET @user_count =;
Call Mybatis.ges_user_count (, @user_count);

Third, edit Usermapper.xml

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

<!-- 
inquiries to get the number of males or females, if the afferent is female otherwise it is male
-->
<select id= "Getusercount" parametermap= " Getusercountmap "statementtype=" callable "> Call
mybatis.ges_user_count" (?,?)
</select>
<!--
Parametermap.put ("Sexid");
Parametermap.put ("UserCount",-);
-->
<parametermap type= "Java.util.Map" id= "Getusercountmap" > <parameter property= "Sexid"
Mode= "in" jdbctype= "integer"/> <parameter property= "UserCount" mode= "Out
" jdbctype= "integer"/>

Iv. Writing Unit test code

 package me.gacl.test; import java.util.HashMap; import java.util.List; Import java.util .
Map;
Import Me.gacl.custom.model.ConditionUser;
Import Me.gacl.domain.User;
Import Me.gacl.util.MyBatisUtil;
Import org.apache.ibatis.session.SqlSession;
Import Org.junit.Test; /** * @author gacl * Test Call stored procedure/public class Test {@Test public void Testgetusercount () {sqlsession sqlsession = MyBatis
Util.getsqlsession (); /** * Maps the identity string of SQL, * Me.gacl.mapping.userMapper is the value of the namespace attribute of the mapper tag in the Usermapper.xml file, * The getusercount is the id attribute value of the Select label, which can be found by the ID attribute value of the select tag to execute the SQL/String statement = " Me.gacl.mapping.userMapper.getUserCount ";//Mapping SQL Identity string map<string, integer> parametermap = new hashmap<
String, integer> ();
Parametermap.put ("Sexid",);
Parametermap.put ("UserCount",-);
Sqlsession.selectone (statement, PARAMETERMAP);
Integer result = Parametermap.get ("UserCount");
SYSTEM.OUT.PRINTLN (result);
Sqlsession.close (); }
}

The above is a small set to introduce the MyBatis Learning Tutorial (vi)-Call the stored procedures, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.