I wish there was a whitespace removal feature for use in the cloud service.

54 views
Skip to first unread message

HeeGu Lee

unread,
Mar 26, 2020, 5:43:43 AM3/26/20
to mybatis-user
Many services are being provided through the cloud these days.
My system was also decided to service through AWS.
These cloud services cost even one packet.

So it would be nice if mybatis also had the option to automatically reduce the whitespace in the XML file to a minimum.

I tried to do this as a plugin.
I read several blogs through Google Ring.
However, since reflect is used every time, it is judged whether the performance of the computer will be slightly slower.
So it would be nice to be able to set the compression of the query using the whitespaceStripper function, which is called when the log is taken, even when running the actual log.

Please review.
Thank you.

@Intercepts ({@ Signature (type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class WhitespaceStripperPlugin implements Interceptor {

    private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory ();
    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory ();
    private static final ReflectorFactory DEFAULT_REFLECTOR_FACTORY = new DefaultReflectorFactory ();

    @Override
    public Object intercept (Invocation invocation) throws Throwable {

        if (invocation.getTarget () instanceof StatementHandler) {
            StatementHandler statementHandler = (StatementHandler) invocation.getTarget ();
            MetaObject metaStatementHandler = MetaObject.forObject (statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
            String originalSql = (String) metaStatementHandler.getValue ("delegate.boundSql.sql");
            metaStatementHandler.setValue ("delegate.boundSql.sql", removeBreakingWhitespace (originalSql));
        }

        return invocation.proceed ();
    }

    protected String removeBreakingWhitespace (String original) { // copy of BaseJdbcLogger
        StringTokenizer whitespaceStripper = new StringTokenizer (original);
        StringBuilder builder = new StringBuilder ();
        while (whitespaceStripper.hasMoreTokens ()) {
            builder.append (whitespaceStripper.nextToken ());
            builder.append ("");
        }
        return builder.toString ();
    }

    @Override
    public Object plugin (Object target) {
        return Plugin.wrap (target, this);
    }

    @Override
    public void setProperties (Properties properties) {
        // no op
    }
}

Iwao AVE!

unread,
Mar 31, 2020, 6:19:57 AM3/31/20
to mybatis-user
Hello HeeGu,

This probably is the same request as #1126.

You might want to try this workaround using custom language driver (not thoroughly tested).

Regards,
Iwao

--
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/4c47bd4e-cc7a-4ca3-86ed-ade406aade2b%40googlegroups.com.

HeeGu Lee

unread,
Mar 31, 2020, 10:47:07 PM3/31/20
to mybatis-user
Oh, It's a another view of solve my problem.
Thank you, Iwao!
Have a nice day!

2020년 3월 31일 화요일 오후 7시 19분 57초 UTC+9, Iwao AVE! 님의 말:
To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com.

HeeGu Lee

unread,
Apr 21, 2020, 5:40:27 AM4/21/20
to mybatis-user
I failed with 1126.
I don't know why I failed... OTL
Bye.


2020년 3월 31일 화요일 오후 7시 19분 57초 UTC+9, Iwao AVE! 님의 말:
Hello HeeGu,

Regards,
Iwao

To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com.

Iwao AVE!

unread,
Apr 21, 2020, 11:51:15 AM4/21/20
to mybatis-user
Could you explain how it didn't work?
Any example so that I can verify the issue?

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/8054292c-4668-4d55-b3b8-0ff29820ae50%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages