1、对应性别查询,该性别的对应人数。
在Mysql中创建存储过程:
①:
#$$ 开始(分隔符)
delimiter $$
#判断是否存在存储过程
drop procedure if exists pro_stu_count$$
#创建存储过程
create procedure pro_stu_count(
in sex varchar(2),
out count int
)
begin
#变量定义
# declare sex varchar(2);
# declare count int;
#into 赋值
select Count(t.id) into count from t_student t where t.sex = sex;
end$$
# $$ 结束 (分隔符)
②:
drop procedure if exists pro_class_student;
create procedure pro_class_student(
in cid varchar(10)
)
begin
select
(select name from t_class c where c.id = ec.class_id) cname,
e.* from t_teacher_class ec,t_teacher e
where ec.class_id = cid and ec.teacher_id = e.id;
end;
2、在idea工程调用存储过程。
xxx.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="procedure">
<!--调用有in out参数的存储过程、返回结果值为普通类型(一个结果值)-->
<!--所有调用存储过程 必须返回一个map 进行接收-->
<!--调用存储过程-->
<!--sex stuCount map中的键 -->
<select id="procedureExample01" parameterType="java.util.Map" statementType="CALLABLE">
CALL pro_stu_count(
#{sex,jdbcType=VARCHAR,mode=IN},
#{stuCount,jdbcType=INTEGER,mode=OUT}
)
</select>
<!--调用存储过程,返回结果集-->
<resultMap id="classTeacherMap" type="java.util.HashMap">
<result column="cid" property="cid" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="sex" property="sex" javaType="java.lang.String" jdbcType="VARCHAR"/>
</resultMap>
<!--resultMap="" 表示用来接收内容-->
<select id="procedureExample02" parameterType="java.util.Map" statementType="CALLABLE" resultMap="classTeacherMap">
CALL pro_class_student(
#{cid,jdbcType=VARCHAR,mode=IN}
)
</select>
</mapper>