MyBatis-调用存储过程

12 篇文章 0 订阅
3 篇文章 0 订阅

1.创建存储过程

--统计员工人数的存储过程
CREATE OR REPLACE PROCEDURE count_emp(p_deptno IN INTEGER,count_total OUT INTEGER,exec_date OUT DATE)
IS
BEGIN
       SELECT COUNT(*) INTO count_total FROM emp WHERE deptno=p_deptno;
       SELECT SYSDATE INTO exec_date FROM dual;
       dbms_output.put_line('部门'||p_deptno||'总人数:'||count_total||',统计日期:'||exec_date); 
END;

2.测试

--测试
DECLARE
v_count INTEGER;
v_date DATE;
BEGIN
count_emp(20,v_count,v_date);
END;

3.测试结果

4.新建对应的POJO

public class ProcedurePOJO {
	private int deptno;
	private int result;
	private Date execDate;
        //set、get方法略
}

5.mapper

public interface ProcedureMapper {
	public void count(ProcedurePOJO pojo);
}

6.mapper.xml

	
<select id="count"
	parameterType="com.yan.po.ProcedurePOJO" statementType="CALLABLE">
	 call count_emp(
			#{deptno,mode=IN,jdbcType=INTEGER},
			#{result,mode=OUT,jdbcType=INTEGER},
			#{execDate,mode=OUT,jdbcType=DATE}
		)
	</select>



7.测试

	ProcedureMapper mapper=sqlSession.getMapper(ProcedureMapper.class);
	ProcedurePOJO pojo=new ProcedurePOJO();
	pojo.setDeptno(10);
	mapper.count(pojo);
	System.out.println(pojo.getDeptno()+"的总人数:"+pojo.getResult());
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	System.out.println(sdf.format(pojo.getExecDate()));

8.备注

这是根据《深入浅出MyBatis》书上代码稍作修改而来,书上所示mapper.xml格式如下:

<select id="count"
	parameterType="com.yan.po.ProcedurePOJO" statementType="CALLABLE">
	 {call count_emp(
			#{deptno,mode=IN,jdbcType=INTEGER},
			#{result,mode=OUT,jdbcType=INTEGER},
			#{execDate,mode=OUT,jdbcType=DATE}
		)}
	</select>

9. 特别指出:

此处的大括号与call之间不能换行(但是可以有空格),后面的大括号可以换行,否则会抛异常:java.sql.SQLException: 出现不支持的 SQL92 标记: 2: 



使用Mybatis-Plus调用存储过程需要先定义一个Mapper接口,然后在接口中定义一个方法,方法名和存储过程名一致,使用@Select注解标注该方法,同时在@Select注解中指定存储过程的名称和参数类型。 例如,假设有一个存储过程名为"get_user_by_id",接收一个int类型的参数,返回一个User对象,那么可以定义一个Mapper接口如下: ``` public interface UserMapper extends BaseMapper<User> { @Select("call get_user_by_id(#{id, mode=IN, jdbcType=INTEGER}, #{result, mode=OUT, jdbcType=CURSOR, resultMap=userResultMap})") void getUserById(@Param("id") int id, @Param("result") ResultSet[] result); } ``` 在@Select注解中,使用"call"关键字指定调用存储过程,然后使用"#{参数名, mode=IN, jdbcType=参数类型}"指定输入参数,"#{参数名, mode=OUT, jdbcType=参数类型, resultMap=结果集映射}"指定输出参数,其中"resultMap"指定输出结果集的映射关系。 调用该方法时,可以使用Mybatis-Plus提供的SqlSessionTemplate执行调用: ``` @Autowired private SqlSessionTemplate sqlSessionTemplate; public User getUserById(int id) { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); ResultSet[] result = new ResultSet[1]; mapper.getUserById(id, result); User user = null; try { if (result[].next()) { user = new User(); user.setId(result[].getInt("id")); user.setName(result[].getString("name")); user.setAge(result[].getInt("age")); } } catch (SQLException e) { e.printStackTrace(); } return user; } ``` 在调用getUserById方法时,先获取UserMapper的实例,然后创建一个ResultSet数组作为输出参数,调用getUserById方法,最后从输出参数中获取结果集并转换为User对象返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值