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
}
}