标签:
create table p_user(
id int primary key auto_increment,
name varchar(10),
sex char(2)
);
insert into p_user(name,sex) values(‘A‘,"男");
insert into p_user(name,sex) values(‘B‘,"女");
insert into p_user(name,sex) values(‘C‘,"男");
#创建存储过程(查询得到男性或女性的数量, 如果传入的是0就女性否则是男性)
DELIMITER $
CREATE PROCEDURE mybatis.ges_user_count(IN sex_id INT, OUT user_count INT)
BEGIN
IF sex_id=0 THEN
SELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex=‘女‘ INTO user_count;
ELSE
SELECT COUNT(*) FROM mybatis.p_user WHERE p_user.sex=‘男‘ INTO user_count;
END IF;
END
$
#调用存储过程
DELIMITER ;
SET @user_count = 0;
CALL mybatis.ges_user_count(1, @user_count);
SELECT @user_count;
public class PUser {
private String id;
private String name;
private String sex;
}
<mapper namespace="cn.imentors.mybatis.test8.userMapper">
<!--
查询得到男性或女性的数量, 如果传入的是0就女性否则是男性
CALL mybatis.get_user_count(1, @user_count);
-->
<select id="getCount" statementType="CALLABLE" parameterMap="getCountMap">
call mybatis.get_user_count(?,?)
</select>
<parameterMap type="java.util.Map" id="getCountMap">
<parameter property="sexid" mode="IN" jdbcType="INTEGER"/>
<parameter property="usercount" mode="OUT" jdbcType="INTEGER"/>
</parameterMap>
</mapper>
@Test
public void test() {
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test8.userMapper.getCount";
Map<String, Integer> parameterMap = new HashMap<String, Integer>();
parameterMap.put("sexid", 1);
parameterMap.put("usercount", -1);
session.selectOne(statement, parameterMap);
//取值
Integer result = parameterMap.get("usercount");
System.out.println(result);
session.close();
}
标签:
原文地址:http://www.cnblogs.com/mentorStudio/p/4810472.html