Created
August 19, 2020 03:14
-
-
Save circlee/d89df73c384f5c51f2cfb284c123ff8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.List; | |
import java.util.Properties; | |
import javax.annotation.PostConstruct; | |
import org.apache.ibatis.cache.CacheKey; | |
import org.apache.ibatis.executor.Executor; | |
import org.apache.ibatis.mapping.BoundSql; | |
import org.apache.ibatis.mapping.MappedStatement; | |
import org.apache.ibatis.mapping.MappedStatement.Builder; | |
import org.apache.ibatis.mapping.ParameterMapping; | |
import org.apache.ibatis.mapping.SqlSource; | |
import org.apache.ibatis.plugin.Interceptor; | |
import org.apache.ibatis.plugin.Intercepts; | |
import org.apache.ibatis.plugin.Invocation; | |
import org.apache.ibatis.plugin.Plugin; | |
import org.apache.ibatis.plugin.Signature; | |
import org.apache.ibatis.session.Configuration; | |
import org.apache.ibatis.session.ResultHandler; | |
import org.apache.ibatis.session.RowBounds; | |
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.databind.introspect.Annotated; | |
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; | |
import lombok.extern.slf4j.Slf4j; | |
@Slf4j | |
@Intercepts ( | |
{ | |
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) | |
, @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}) | |
} | |
) | |
public class SqlIdCommentInjector implements Interceptor { | |
public SqlIdCommentInjector() { | |
init(); | |
} | |
@PostConstruct | |
public void init() { | |
} | |
@Override | |
public Object intercept(Invocation invocation) throws Throwable { | |
Object[] args = invocation.getArgs(); | |
MappedStatement ms = (MappedStatement)args[0]; | |
MappedStatement wrapped = copyFromMappedStatement(ms, new SqlSourceWrap(ms)); | |
args[0] = wrapped; | |
return invocation.proceed(); | |
} | |
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { | |
Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms .getId(), newSqlSource, ms.getSqlCommandType()); | |
builder.resource(ms.getResource()); | |
builder.fetchSize(ms.getFetchSize()); | |
builder.statementType(ms.getStatementType()); | |
builder.keyGenerator(ms.getKeyGenerator()); | |
builder.timeout(ms.getTimeout()); | |
builder.parameterMap(ms.getParameterMap()); | |
builder.resultMaps(ms.getResultMaps()); | |
builder.resultSetType(ms.getResultSetType()); | |
builder.cache(ms.getCache()); | |
builder.flushCacheRequired(ms.isFlushCacheRequired()); | |
builder.useCache(ms.isUseCache()); | |
return builder.build(); | |
} | |
private class SqlSourceWrap implements SqlSource { | |
private final MappedStatement deletageMappedStatement; | |
private final SqlSource delegateSqlSource; | |
private final String id; | |
public SqlSourceWrap(MappedStatement ms) { | |
this.deletageMappedStatement = ms; | |
this.delegateSqlSource = ms.getSqlSource(); | |
this.id = ms.getId(); | |
} | |
@Override | |
public BoundSql getBoundSql(Object parameterObject) { | |
BoundSql origin = this.delegateSqlSource.getBoundSql(parameterObject); | |
return new BoundSqlWrap(deletageMappedStatement.getConfiguration(), origin, id); | |
} | |
class BoundSqlWrap extends BoundSql { | |
private final BoundSql delegate; | |
private final String id; | |
public BoundSqlWrap(Configuration conf, BoundSql boundSql, String id) { | |
super(conf, boundSql.getSql(), boundSql.getParameterMappings(), boundSql.getParameterObject()); | |
this.delegate = boundSql; | |
this.id = id; | |
} | |
public String getSql() { | |
return "/*" + id + "*/\r\n" + this.delegate.getSql(); | |
} | |
public List<ParameterMapping> getParameterMappings() { | |
return this.delegate.getParameterMappings(); | |
} | |
public Object getParameterObject() { | |
return this.delegate.getParameterObject(); | |
} | |
public boolean hasAdditionalParameter(String name) { | |
return this.delegate.hasAdditionalParameter(name); | |
} | |
public void setAdditionalParameter(String name, Object value) { | |
this.delegate.setAdditionalParameter(name, value); | |
} | |
public Object getAdditionalParameter(String name) { | |
return this.delegate.getAdditionalParameter(name); | |
} | |
} | |
} | |
@Override | |
public Object plugin(Object target) { | |
return Plugin.wrap(target, this); | |
} | |
@Override | |
public void setProperties(Properties properties) { | |
// nothing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment