[英]How to get server messages (raise notice) from PostgreSQL function that was invoked through MyBatis mapper method?
运行普通 JDBC 语句时,可以使用Statement.getWarnings()
获取通过 PostgreSQL function 中的raise notice
命令生成的 output 消息,例如:
try (CallableStatement callableStatement = connection.prepareCall("select my_func()")) {
callableStatement.execute();
// ... do something with the output ...
SQLWarning sqlWarning = callableStatement.getWarnings();
while (sqlWarning != null) {
System.out.println(sqlWarning.getMessage());
sqlWarning = sqlWarning.getNextWarning();
}
}
有没有办法在运行 MyBatis 映射器方法后获取相同的 output,而不求助于原始 JDBC?
除非建议更好的选择,否则我能够使用 MyBatis 拦截器获取 output。 拦截器代码:
@Intercepts({
@Signature(type = StatementHandler.class, method = "update", args = {Statement.class})})
public class FetchStatementWarningsInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
if (args == null || args.length != 1 || !(args[0] instanceof Statement)) {
return invocation.proceed();
}
Statement statement = (Statement) args[0];
Object result = invocation.proceed();
if (!statement.isClosed()) {
SQLWarning sqlWarning = statement.getWarnings();
// ... do something with the warnings output ...
}
return result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.