When running a plain JDBC statement, the output messages produced through raise notice
commands in a PostgreSQL function can be fetched using Statement.getWarnings()
, eg:
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();
}
}
Is there a way to fetch that same output after running a MyBatis mapper method, without resorting to raw JDBC?
Unless a better option is suggested, I was able to fetch that output using a MyBatis interceptor. The interceptor code:
@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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.