Mybaits(14)存储过程
Posted xhbjava
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybaits(14)存储过程相关的知识,希望对你有一定的参考价值。
一、MyBatis存储过程调用
1.创建存储过程
CREATE PROCEDURE save_user_info ( OUT id INTEGER, IN user_name VARCHAR ( 20 ), IN real_name VARCHAR ( 20 ), IN sex VARCHAR ( 20 ), IN moble VARCHAR ( 20 ), IN email VARCHAR ( 20 ), IN note VARCHAR ( 20 ) ) BEGIN INSERT INTO t_user ( user_name, real_name, sex, moble, email, note ) VALUES ( user_name, real_name, sex, moble, email, note ); SET id = LAST_INSERT_ID( ); END
OUT:出参
IN:入参
2.持久层Dao层增加方法
/** * 保存用户 * @param user */ public void saveUser(User user);
3.持久层配置文件编写
IUserDao.xml
<insert id="saveUser" parameterType="userMapper" statementType="CALLABLE"> {call save_user_info(#{id,mode=OUT,jdbcType=INTEGER},#{userName,mode=IN},#{realName,mode=IN},#{sex,mode=IN},#{moble,mode=IN},#{email,mode=IN},#{note,mode=IN})} </insert>
4.编写测试类
@Test public void saveUser() { // 5.创建Dao的代理对象 session = factory.openSession(true); userDao = session.getMapper(IUserDao.class); User u = new User(); u.setUserName("詹三"); u.setRealName("张三"); u.setSex(null); u.setMoble("12121212"); u.setEmail("1212@qq.com"); u.setNote("备注"); int id = userDao.saveUser(u); System.out.println("==="+id); }
CREATE PROCEDURE save_user_info (OUT id INTEGER,IN user_name VARCHAR ( 20 ),IN real_name VARCHAR ( 20 ),IN sex VARCHAR ( 20 ),IN moble VARCHAR ( 20 ),IN email VARCHAR ( 20 ),IN note VARCHAR ( 20 ) ) BEGININSERT INTO t_user ( user_name, real_name, sex, moble, email, note )VALUES( user_name, real_name, sex, moble, email, note );
SET id = LAST_INSERT_ID( );
END
以上是关于Mybaits(14)存储过程的主要内容,如果未能解决你的问题,请参考以下文章