在用mybatis调用数据库的存储过程时,我发现有时即使mybatis失败了,乃至存储过程异常了。mybatis也不会抛出异常,下面是处理这种情况的方法
1. 有如下存储过程( sqlServer 2012)
USE [WES]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Pros_CreateMessage]
@id varchar(50) = '',
@senderId varchar(50) = 'A0000001',
@type int
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRAN
BEGIN TRY
--存储过程具体实现代码.....
set @result = 1 --如果成功就返回的结果
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
print(ERROR_MESSAGE())
set @result = ERROR_MESSAGE() --失败就将异常抛出,到时mybatis就会收到
end catch
select @result as succeed --正常结束时就返回@result
END
只要将ERROR_MESSAGE()做为返回值,到时mybatis自动就会抛出异常了
2. mybatis存储过程的调用
<select id="xxx" resultType="int" parameterType="map" statementType="CALLABLE" flushCache="true">
{call Pros_CreateMessage(#{id,mode=In,jdbcType=VARCHAR},#{senderId,mode=In,jdbcType=VARCHAR})}
</select>