欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis调用mysql存储过程 mysql

程序员文章站 2022-05-29 13:26:19
...
mybatis调用mysql存储过程

1.mybastis的配置
<select id="grabRedPacketProcedure">
    call grabRedPacketProcedure(?,?,?)
    </select>&lt;parametermap type="java.util.Map" id="paramMap"&gt;&lt;parameter property="redPacketId" mode="IN" javatype="INTEGER"&gt;&lt;/parameter&gt;&lt;parameter property="userId" mode="IN" javatype="INTEGER"&gt;&lt;/parameter&gt;&lt;parameter property="num" mode="OUT" javatype="INTEGER" jdbctype="INTEGER"&gt;&lt;/parameter&gt;&lt;/parametermap&gt;

2.mapper
@Mapper
public interface RedPacketMapper {

public int insertRedPacketUser(RedPacketUserPO po);

//调用存储过程
public void grabRedPacketProcedure(Map&lt;string integer&gt; map);
}
   
3.mysql存储过程
delimiter //
DROP PROCEDURE IF EXISTS grabRedPacketProcedure
//
create PROCEDURE grabRedPacketProcedure(IN v_red_packet_id INTEGER,IN v_user_id INTEGER,OUT num INTEGER)
begin
set num:= -1;
select @total:=stock from red_packet where id = v_red_packet_id;
if @total &amp;gt; 0 then
begin
start transaction;
update red_packet set stock=stock-1 where id = v_red_packet_id;
insert into red_packet_user(red_packet_id,user_id,amount) values (v_red_packet_id,v_user_id,5);
commit;
set num:= 1;
end;
end if;
end
//
DELIMITER ;

4.存储过程的调用
public int grapRedPacketProc(Integer redPacketId, Integer userId) {
int num = -1;
Map&lt;string&gt; map = new HashMap&lt;string integer&gt;();
redPacketMapper.grabRedPacketProcedure(map);
if(map.containsKey("num")) {
num = map.get("num");
}
return num;
}
&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;
相关标签: mysql