项目结构

MyBatis 调用存储过程(详解)-DESTLIVE

数据表t_user

MyBatis 调用存储过程(详解)-DESTLIVE

创建User

package com.po;
public class User {
	private Integer id;
	private String name;
	private String sex;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
}

创建UserMapper

package com.mapper;
import com.po.User;
public interface UserMapper {
	//增加用户
	void addUser(User user);	
	//删除用户
	void deleteUser(Integer id);
	//根据id查询用户
	User getUserById(Integer id);
	//更新用户
	void updateUser(User user);
}

创建insert_user存储过程

CREATE PROCEDURE insert_user(OUT u_id INTEGER,IN u_name VARCHAR(20),IN u_sex VARCHAR(20),IN u_age INTEGER)
BEGIN
INSERT INTO t_user (name,sex,age) VALUES (u_name,u_sex,u_age);
SET u_id=LAST_INSERT_ID();
END

在UserMapper.xml中调用insert_user存储过程

<!-- 添加用户 -->
	<insert id="addUser" parameterType="com.po.User" statementType="CALLABLE">
		{call insert_user(
		#{id,mode=OUT,jdbcType=INTEGER},#{name,mode=IN},#{sex,mode=IN},#{age,mode=IN})}
	</insert>

创建deleteUser存储过程

CREATE PROCEDURE deleteUser(IN u_id INTEGER)
BEGIN
    DELETE FROM t_user WHERE id=u_id;
END

在UserMapper.xml中调用deleteUser存储过程

<!-- 删除用户 -->
	<delete id="deleteUser" parameterType="Integer" statementType="CALLABLE">
		{call deleteUser(#{id,mode=IN})}
	</delete>

创建updateUser存储过程

CREATE PROCEDURE updateUser(IN u_id INTEGER,IN u_name VARCHAR(20),IN u_sex VARCHAR(20),IN u_age INTEGER)
BEGIN
    UPDATE t_user SET name=u_name,sex=u_sex,age=u_age WHERE id=u_id;
END

在UserMapper.xml中调用updateUser存储过程

<!-- 更新用户 -->
	<update id="updateUser" parameterType="user" statementType="CALLABLE">
		{call updateUser(#{id,mode=IN},#{name,mode=IN},#{sex,mode=IN},#{age,mode=IN})}
	</update>

创建getUserById存储过程

CREATE PROCEDURE getUserById(IN u_id INTEGER)
BEGIN
    SELECT id,name,sex,age FROM t_user WHERE id=u_id;
END

在UserMapper.xml中调用getUserById存储过程

<!-- 根据id查询用户 -->
	<select id="getUserById" parameterType="Integer" resultType="user" statementType="CALLABLE">
		{call getUserById(#{id,mode=IN})}
	</select>

创建UserDao

package com.dao;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.mapper.UserMapper;
import com.po.User;
public class UserDao {
	private SqlSession	session=null;
	//获取SqlSession对象
	public SqlSession getSqlSession() throws IOException {
		InputStream	is = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		return sessionFactory.openSession();
	}
	@Test
	public void addUser() throws IOException {
		session=getSqlSession();
		UserMapper um=session.getMapper(UserMapper.class);
		User user=new User();	
		user.setName("jack");
		user.setSex("男");
		user.setAge(20);
		um.addUser(user);
		System.out.println("添加成功,增加的id="+user.getId());
		session.commit();
		session.close();
	}
	@Test
	public void getUserById() throws IOException {
		session=getSqlSession();
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.getUserById(2);
		session.close();
		System.out.println(user);
	}
	@Test
	public void updateUser() throws IOException {
		session=getSqlSession();
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.getUserById(2);
		user.setName("john");
		user.setSex("男");
		user.setAge(30);
		um.updateUser(user);
		session.commit();
		session.close();
	}
	@Test
	public void deleteUser() throws IOException {
		session=getSqlSession();
		UserMapper um=session.getMapper(UserMapper.class);
		um.deleteUser(3);
		session.commit();
		session.close();
	}
}

执行UserDao的addUser()方法:

MyBatis 调用存储过程(详解)-DESTLIVE
MyBatis 调用存储过程(详解)-DESTLIVE

执行UserDao的deleteUser()方法:

MyBatis 调用存储过程(详解)-DESTLIVE
MyBatis 调用存储过程(详解)-DESTLIVE

执行UserDao的updateUser()方法:

MyBatis 调用存储过程(详解)-DESTLIVE
MyBatis 调用存储过程(详解)-DESTLIVE

执行UserDao的getUserById()方法:

MyBatis 调用存储过程(详解)-DESTLIVE