码迷,mamicode.com
首页 > 其他好文 > 详细

Mybaits(14)存储过程

时间:2020-03-03 15:13:00      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:save   ram   入参   配置文件   ===   div   文件   图片   持久层   

一、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)存储过程

标签:save   ram   入参   配置文件   ===   div   文件   图片   持久层   

原文地址:https://www.cnblogs.com/xhbJava/p/12402079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!