簡體   English   中英

如何從 PostgreSQL function 獲取通過 MyBatis mapper 方法調用的服務器消息(raise notice)?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM