Always get a return value when using a custom interceptor for query

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Always get a return value when using a custom interceptor for query

chenmingmingxm
I am using spring boot 2.0.3.RELEASE. I tried to write an interceptor when the query is executed.When I finished writing, I found that although the code of the interceptor was implemented, I still couldn’t get the return value.
This  is the code.

```java
@Intercepts({
        @Signature(
                type = Executor.class,method = "query",
                args = {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class}
        )
})
@Component
public class PageInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        final Object[] args = invocation.getArgs();
        ResultHandler handler = e ->{
            System.out.println(e.getResultObject());
        };
        args[3] =  handler;


        Object proceed = invocation.proceed();

        return proceed;
    }
    ...
}
```
The data is printed, but the result is empty.

When I debug the code i have some questions.This is the code of mybatis

```java

package org.apache.ibatis.executor.resultset;

DefaultResultSetHandler

private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {
      if (parentMapping != null) {
        handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
      } else {
        //When no ResultHandler is specified   multipleResults.add(defaultResultHandler.getResultList());
        if (resultHandler == null) {
          DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
          multipleResults.add(defaultResultHandler.getResultList());
        } else {
          //this  multipleResults is empty
          handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
        }
      }
    } finally {
      // issue #228 (close resultsets)
      closeResultSet(rsw.getResultSet());
    }
  }
```
I don't know if it is caused by this reason.When I specified the DefaultResultSetHandler.How to get the results?





--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Always get a return value when using a custom interceptor for query

Erwan Letessier
Custom ResultHandler here writes only in the console unlike default result handler that indirectly writes in a list that is eventually returned.
It would to know what you want to achieve by using interceptor.

On Mon, Sep 24, 2018, 07:18 <[hidden email]> wrote:
I am using spring boot 2.0.3.RELEASE. I tried to write an interceptor when the query is executed.When I finished writing, I found that although the code of the interceptor was implemented, I still couldn’t get the return value.
This  is the code.

```java
@Intercepts({
        @Signature(
                type = Executor.class,method = "query",
                args = {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class}
        )
})
@Component
public class PageInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        final Object[] args = invocation.getArgs();
        ResultHandler handler = e ->{
            System.out.println(e.getResultObject());
        };
        args[3] =  handler;


        Object proceed = invocation.proceed();

        return proceed;
    }
    ...
}
```
The data is printed, but the result is empty.

When I debug the code i have some questions.This is the code of mybatis

```java

package org.apache.ibatis.executor.resultset;

DefaultResultSetHandler

private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {
      if (parentMapping != null) {
        handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
      } else {
        //When no ResultHandler is specified   multipleResults.add(defaultResultHandler.getResultList());
        if (resultHandler == null) {
          DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
          multipleResults.add(defaultResultHandler.getResultList());
        } else {
          //this  multipleResults is empty
          handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
        }
      }
    } finally {
      // issue #228 (close resultsets)
      closeResultSet(rsw.getResultSet());
    }
  }
```
I don't know if it is caused by this reason.When I specified the DefaultResultSetHandler.How to get the results?





--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.