03-MyBatis调用存储过程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linzhaoliangyan/article/details/88661256

需求:
查询得到男性或女性的数量, 如果传入的是0就女性否则是男性

存储过程
DELIMITER $
CREATE PROCEDURE hx01.student_count(IN sex INT, OUT count INT)
BEGIN  
IF sex=0 THEN
SELECT COUNT(*) FROM hx01.student WHERE student.ssex='女' INTO count;
ELSE
SELECT COUNT(*) FROM hx01.student WHERE student.ssex='男' INTO count;
END IF;
END 
$

-- 调用存储过程
DELIMITER ;
SET @count = 0;
CALL hx01.student_count(1, @count);
SELECT @count;

 /**
     * 通过性别查询人数
     * @param map
     * @return
     */
    public void getStudentCountBySex(HashMap<String,Integer> map);

<select id="getStudentCountBySex" parameterMap="pmap" statementType="CALLABLE">
        CALL hx01.student_count(?,?);
    </select>
    
    <parameterMap id="pmap" type="java.util.Map">
        <parameter property="sex" jdbcType="INTEGER" mode="IN"></parameter>
        <parameter property="count" jdbcType="INTEGER" mode="OUT"></parameter>
    </parameterMap>


@Test
    public void test11(){
        SqlSession session = MyBatisUtils.getSqlSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        HashMap<String,Integer> map=new HashMap<String,Integer>();
        map.put("sex",1);
        map.put("count",-1);
        mapper.getStudentCountBySex(map);
        System.out.println(map.get("count"));
        session.close();
    }

猜你喜欢

转载自blog.csdn.net/linzhaoliangyan/article/details/88661256