您的位置:首页 > 数据库

mybatis调用 存储过程方法简单demo

2017-11-17 14:44 531 查看
以下是一次简单 的存储过程调用并且在sql中使用行级锁,

本例仅为演示,实际操作请尽量使用spring事务进行管理

Mapper接口类

public interface ???Mapper {
public void getCheckOutLock(CheckOutProduce checkOutProduce);
}

存储过程

CREATE PROCEDURE get_check_out_lock(IN `receptionId` bigint,OUT `result` int)
BEGIN
#Routine body goes here...
DECLARE record_version int ;
#开启事务
START TRANSACTION;
#加入排他锁
SELECT version from reception where reception_id = receptionId INTO record_version for update;
#该条记录未被锁定
if(record_version = 0) then
set result = 1;
UPDATE reception SET version = 1 where reception_id = receptionId ;
else
set result = 0;
end if;
COMMIT;
END


mapper.xml文件写法
<select id="getCheckOutLock" parameterType="com.kzhotel.pojo.CheckOutProduce"
statementType="CALLABLE" resultType="com.kzhotel.pojo.CheckOutProduce">
{call get_check_out_lock (#{receptionId,jdbcType=INTEGER,mode=IN},#{result,jdbcType=INTEGER,mode=OUT})}
</select>
实体类定义

package com.kzhotel.pojo;

public class CheckOutProduce {

private Long receptionId;

private Integer result;

public Long getReceptionId() {
return receptionId;
}

public void setReceptionId(Long receptionId) {
this.receptionId = receptionId;
}

public Integer getResult() {
return result;
}

public void setResult(Integer result) {
this.result = result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis sql 存储过程