一、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("[email protected]"); 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
原文地址:https://www.cnblogs.com/xhbJava/p/12402079.html
时间: 2024-10-10 17:46:26