MyBatis实践笔记(2):调用存储过程

一、MyBatis调用存储过程
  MyBatis调用存储过程的方式,和普通的select查询方式用法相同,都可以接收方法入参(parameterType = String | Object | Map)、和方法返回值(resultType | resultMap)。
  在mapper.xml文件中,定义存储过程时,有一点不同的是需要增加一项设置:statementType=“CALLABLE",以表明这是一个存储过程
 
步骤一:在数据库中创建存储过程pro_refresh_data()
//在数据库中创建存储过程
CREATE PROCEDURE pro_refresh_data()
BEGIN
insert into user_account (account_id,user_id,balance) 
values ('6123-0002','--','1000');
update user_account set user_id='渐渐老了' where account_id='6123-0002'; insert into user_stock (stock_id,stock_name,user_id,count_num) VALUES ('123456','京东金融','渐渐老了','100'); select * from user_account; end

步骤二、在
mapper.xml配置文件中,编写存储过程调用语句:
//编写配置文件:mapper.xml
<mapper namespace="com.newbie.dao.ProcedureDAO">
  <!--注意,在mapper-select节点配置时,需要增加一项设置:statementType=“CALLABLE",以表明这是一个存储过程 -->
  <select id="callProcedure" statementType="CALLABLE" resultType="Integer">
    {call pro_refresh_data()}
  </select>
</mapper>


步骤三、编写调用存储过程的DAO

//编写调用存储过程的DAO
public interface ProcedureDAO {
    //如果存储过程有入参,可接受入参参数(规则与普通的select语句相同)
    int callProcedure();
}


步骤四、客户端调用存储过程

//客户端调用存储过程
public void testCallProcedure(){
    procedureDAO.callProcedure();
}





 
 
 
原文地址:https://www.cnblogs.com/newbie27/p/10835662.html