Best approach for inserting logic before and after the execution of a query ?

17 views
Skip to first unread message

Francisco Herrera

unread,
May 21, 2020, 3:19:10 PM5/21/20
to mybatis-user

Hi, we need to port some old functionality from Ibatis into Mybatis, in which some logic is inserted before and after the execution of a query. We want to assess what would be a better approach from this two (or any other?):

a) Using a Mybatis plugin such as:

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



b) Extending SqlSession such as:

public class MyNewSqlMapClientTemplate extends SqlSessionTemplate { <-- We are using Spring Mybatis


To give some context, on the old Ibatis, the code used to look like:

   @Override
    public Object queryForObject(String statementName, Object parameterObject)
            throws DataAccessException {

        return execute(new ScopedSqlMapClientCallback(statementName, parameterObject,
                new ExtendedSqlMapClientCallback() {

                    @Override
                    public Object callback(SqlMapExecutor executor, String statementName, Object params)
                            throws SQLException {
                        return executor.queryForObject(statementName, params);
                    }
                }));
    }

Thanks,
Francisco

Guy Rouillier

unread,
May 21, 2020, 6:18:59 PM5/21/20
to mybati...@googlegroups.com
You don't mention which DBMS you are using, or what kind of logic you need to apply.  One approach I've used with Oracle was to employ an anonymous procedure block within the mapper SQL.  This approach is of course limited to the capabilities of the stored procedure implementation, but for simple needs works well and is simple to implement.

--
Guy Rouillier
--
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 mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com.

Francisco Herrera

unread,
May 22, 2020, 12:06:23 AM5/22/20
to mybatis-user
Hi Guy,

We use Oracle, and the ad-hoc logic is as follows. For all SQL/queries the system needs to:

1) Find if the SQL/query has variables with a special format $variable_X$
2) If so, run a special SQL/query that will establish some context on the DB side, and will return values to be passed for those $variable_X$ variables
3) Then, run the original SQL with the values fetched above for the $variable_X$ variables
4) And finally, run a final SQL/query to clean the context on DB side

The Mybatis plugin seems to be doing the job, but we are not sure if a plugin has any cons for this kind of logic described.

Any ideas ?

Francisco
To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages