数据库
首页 > 数据库> > mysql存储过程的使用案例

mysql存储过程的使用案例

作者:互联网

循环插入数据

1.创建存储过程

CREATE DEFINER=`social`@`%` PROCEDURE `inser_gift_coupon`(IN in_number INT,IN in_user_id BIGINT)
BEGIN
DECLARE count INT default 0;
DECLARE sum INT default 0;
    WHILE count < in_number DO
    INSERT INTO `social_db`.`gift_coupon` ( `user_id`, `gift_coupon_id`, `life`, `create_time` )
    VALUES
        (in_user_id, 'T001', 3162240000, now()),
        (in_user_id, 'T002', 3162240000, now());

SET sum = sum + count;
SET count = count + 1;
END WHILE;
select sum;

END

参数说明 

in_number 要插入多少条
in_user_id 用户ID

查看存储过程
show procedure status;
 

2. mybatis调用

<select id="batchAddGiftCoupon" statementType="CALLABLE" resultType="java.lang.Integer">
        {call inser_gift_coupon(#{inNumber,mode=IN,jdbcType=INTEGER},#{inUserId,mode=IN,jdbcType=BIGINT})}
</select>
int batchAddGiftCoupon(@Param("inNumber") Integer inNumber, @Param("inUserId") Long inUserId);

 

 

标签:count,存储,gift,coupon,sum,案例,user,mysql,id
来源: https://www.cnblogs.com/xmcog/p/14096541.html