1. Make a demand
Query the number of males or females, if the incoming is 0, females otherwise males
2. Prepare 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 stored procedure (query to get the number of male or female, if the incoming is female, otherwise it is male)DELIMITER $CREATE PROCEDURE mybatis.ges_user_count(IN sex_id INT, OUT user_count INT)BEGIN IF sex_id= THENSELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex='female' INTO user_count;ELSELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex='male' INTO user_count;END IF;END $-- Call stored procedure DELIMITER ;SET @user_count = ;CALL mybatis.ges_user_count(, @user_count);SELECT @user_count; 3. Edit userMapper.xml
Edit the userMapper.xml file and add the following configuration items
<!-- Query gets the number of men or women, if the incoming is female, otherwise it is a 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"//</parameterMap> 4. Write 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 procedures*/public class Test {@Testpublic void testGetUserCount(){SqlSession sqlSession = MyBatisUtil.getSqlSession();/*** The identification string for mapping sql, * me.gacl.mapping.userMapper is the value of the namespace attribute of the mapper tag in the userMapper.xml file, * getUserCount is the id attribute value of the select tag. The SQL to be executed can be found through the id attribute value of the select tag. */String statement = "me.gacl.mapping.userMapper.getUserCount";//The identification string for mapping sql 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 the MyBatis learning tutorial (VI) introduced to you by the editor - Calling the stored procedure. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!