MyBatis:从 SQL 服务器捕获消息抛出存储过程
MyBatis : Catch message throw stored procedure from SQL Server
我正在使用 Spring、MyBatis 和 Microsoft SQL 服务器。在我的项目中,我需要从 MyBatis mapper xml 文件中调用一个过程。
所以,我按照下面的方式做了:
<select id="callProcedure" parameterType="TestBean" statementType="CALLABLE" >
{CALL callprocedure(#{id},#{regId})}
</select>
而且,这运作良好。后来,我在我的存储过程中添加了 THROW
语句,如下所示:
THROW 50002, "Invalid Parameters", 1;
而且,我在 my DAO java class 中捕获异常如下:
try {
myMapper.callProcedure(testBean);
} catch (Exception e) {
System.out.println(e.getMessage());
}
那,主要问题就在这里。当打印出消息时,它显示了很多行和原因。不仅是我抛出的消息。我只想要我抛出的消息。
我无法通过调用 e.getMessage()
.
获取从过程中抛出的消息
这会显示以下消息:
org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause:com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters
### The error may exist in mybatis/mapper/application/test.xml
### The error may involve com.test.core.mapper.application.TestMapper.callProcedure-Inline
### The error occurred while setting parameters
### SQL: {CALL callprocedure(?,?)}
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters; uncategorized SQLException for SQL []; SQL state [S0001]; error code [50002]; Invalid Parameters; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters
...
我很难从过程中获取抛出消息。我该如何解决这个问题?
或者还有其他方法可以像这样工作吗?请帮帮我。
谢谢。
在这里,我得到了答案。当我按照以下步骤尝试时,我收到了消息。
try {
myMapper.callProcedure(testBean);
} catch (Exception e) {
System.out.println(e.getCause().getMessage());
}
这解决了我的问题。
我正在使用 Spring、MyBatis 和 Microsoft SQL 服务器。在我的项目中,我需要从 MyBatis mapper xml 文件中调用一个过程。
所以,我按照下面的方式做了:
<select id="callProcedure" parameterType="TestBean" statementType="CALLABLE" >
{CALL callprocedure(#{id},#{regId})}
</select>
而且,这运作良好。后来,我在我的存储过程中添加了 THROW
语句,如下所示:
THROW 50002, "Invalid Parameters", 1;
而且,我在 my DAO java class 中捕获异常如下:
try {
myMapper.callProcedure(testBean);
} catch (Exception e) {
System.out.println(e.getMessage());
}
那,主要问题就在这里。当打印出消息时,它显示了很多行和原因。不仅是我抛出的消息。我只想要我抛出的消息。
我无法通过调用 e.getMessage()
.
这会显示以下消息:
org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause:com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters
### The error may exist in mybatis/mapper/application/test.xml
### The error may involve com.test.core.mapper.application.TestMapper.callProcedure-Inline
### The error occurred while setting parameters
### SQL: {CALL callprocedure(?,?)}
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters; uncategorized SQLException for SQL []; SQL state [S0001]; error code [50002]; Invalid Parameters; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Parameters
...
我很难从过程中获取抛出消息。我该如何解决这个问题?
或者还有其他方法可以像这样工作吗?请帮帮我。
谢谢。
在这里,我得到了答案。当我按照以下步骤尝试时,我收到了消息。
try {
myMapper.callProcedure(testBean);
} catch (Exception e) {
System.out.println(e.getCause().getMessage());
}
这解决了我的问题。