MySql教程

Mybatis 调用mysql存储过程

本文主要是介绍Mybatis 调用mysql存储过程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mybatis 调用mysql存储过程

创建存储过程

DELIMITER //
CREATE PROCEDURE count_student_by_score(IN score VARCHAR(20),IN cid VARCHAR(20) ,OUT number VARCHAR(20))
BEGIN
	SELECT COUNT(*) INTO number FROM score WHERE 
	s_score > score AND c_id = cid;
END //

DELIMITER //
CREATE PROCEDURE `count_teacher_by_name`(IN in_name VARCHAR(20),OUT out_count INT)
BEGIN 
SELECT COUNT(*) INTO out_count FROM teacher
WHERE teacher.`t_name` = in_name;
END//

Mybatis调用存储过程方式一(Map集合)

mapper.xml
	<parameterMap id="stuMap" type="java.util.Map">
        <parameter property="score" mode="IN" jdbcType="VARCHAR"/>
        <parameter property="cid" mode="IN" jdbcType="VARCHAR"/>
        <parameter property="number" mode="OUT" jdbcType="VARCHAR"/>
    </parameterMap>
    <select id="countStudentByScore" statementType="CALLABLE" parameterMap="stuMap">
        call count_student_by_score(?,?,?)
    </select>

mapper.java

@Mapper
public interface ScoreMapper  {
    void countStudentByScore(Map map);
}

Mybatis调用存储过程方式二(实体类)

mapper.xml
<select id="countTeacherByName" statementType="CALLABLE">
    {
    call count_teacher_by_name(#{in_name ,mode=IN ,jdbcType=VARCHAR},
    #{out_count ,mode=OUT ,jdbcType=VARCHAR})
    }
</select>

mapper.java

@org.apache.ibatis.annotations.Mapper
public interface TeacherMapper extends Mapper<Teacher> {
    void countTeacherByName(TeacherVo teacherVo);
}
这篇关于Mybatis 调用mysql存储过程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!