mysql的存储过程会有三种参数,此文章只讨论in和out参数。本文章已创建用户为例子,因为创建用户需要用到左右层级关系。所以需要使用存储过程,为了方便演示,本文只以创建简单的用户记录为主,不研究为什么使用。
一 创建mysql存储过程,存储过程中有两个参数(in usernme,out result_id)
使用navicat做工具
insert into f_user(username) values(p_username);
select @@identity into result_id;
END
说明:select @@identity into result_id; 表示将插入的id返回到result_id参数中
二 mybatis使用
1 定义一个用户存入类
@Data
public class UserTestAddIndto {
//用于接收返回的id参数
private Integer resultId;
//输入参数
private String username;
}
2 在xml中使用存储过程(重点)
<insert id="insert" statementType="CALLABLE" >
{call add_sub_node(#{username},#{resultId,jdbcType=INTEGER,mode=OUT})}
</insert>
3 使用这个方法,就会将result_id存入到UserTestAddIndto实例中