How to execute a stored procedure in MyBatis?
There are typically two ways to execute stored procedures in MyBatis.
- choose
<select id="callProcedure" statementType="CALLABLE">
{call procedure_name(#{param1, mode=IN, jdbcType=INTEGER}, #{param2, mode=OUT, jdbcType=INTEGER})}
</select>
In this example, callProcedure is a method defined in the mapper file, procedure_name is the name of the stored procedure to be called, and param1 and param2 are the parameters of the stored procedure.
- – make an update
<update id="callProcedure" statementType="CALLABLE">
{call procedure_name(#{param1, mode=IN, jdbcType=INTEGER}, #{param2, mode=OUT, jdbcType=INTEGER})}
</update>
In this example, callProcedure is a method defined in the mapper file, procedure_name is the name of the stored procedure to call, param1 is the input parameter for the stored procedure, and param2 is the output parameter for the stored procedure.
No matter which method is used, it is necessary to define the corresponding SQL statement in the MyBatis mapper file, and specify the input and output types of the parameters. When calling a stored procedure, CALLABLE must be used as the statementType to specify the calling method of the stored procedure.