Mybatis learning notes-call stored procedures

Source: Internet
Author: User

Requirement: query the number of male or female. If the input value is 0, the number of male is obtained. If the input value is 1, the number of female is obtained.

Database Name mybatis database table users

create table users(          id int primary key auto_increment,          name varchar(10),         sex char(2));


Create a stored procedure

Delimiter $ create procedure mybatis. ges_user_count (in sex_id int, out user_count INT) begin if sex_id = 0 thenselect count (*) from mybatis. users where users. sex = 'femal' into user_count; elseselect count (*) from mybatis. users where users. sex = 'male' into user_count; end if; end $

Calling stored procedures in MySQL Console

SET @user_count = 0;CALL mybatis.ges_user_count(1,@user_count);[email protected]_count;


<select id="getUserCount" parameterMap="getUserCountMap" statementType="CALLABLE">call mybatis.get_user_count(?,?)</select><parameterMap type="java.util.Map" id="getUserCountMap"><parameter property="sexid" mode="IN" jdbcType="INTEGER"/><parameter property="usercount" mode="OUT" jdbcType="INTEGER"/></parameterMap>


Test

SqlSessionFactory factory = MybatisUtil.getFactory();SqlSession session = factory.openSession();String s = "com.mybatis.test06.userMapper.getUserCount";Map<String, Integer>parameterMap = new HashMap<String, Integer>();parameterMap.put("sexid", 0);parameterMap.put("usercount", -1);session.selectOne(s, parameterMap);Integer result = parameterMap.get("usercount");System.out.println(result);session.close();


This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1559400

Mybatis learning notes-call stored procedures

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.