MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强 MyBatis 的功能。
插件调用流程如下图:
下面我们利用其插件机制实现替换其sql参数为真实的值插件。
package com.march.common.mybatis.plugin;
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.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Properties;
/**
* @see: MybatisSqlFormatPlugin
* @createTime: 2020/5/22 19:44
* @version:1.0
*/
@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}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
Object.class})
})
public class MybatisSqlFormatPlugin implements Interceptor {
static int MAPPED_STATEMENT_INDEX = 0;
static int PARAMETER_INDEX = 1;
private static final Logger logger = LoggerFactory.getLogger(MybatisSqlFormatPlugin.class);
private Properties properties;
@Override
public Object intercept(Invocation invoker) throws Throwable {
long startTime = System.currentTimeMillis();
try{
return invoker.proceed();
}finally {
Object[] queryArgs = invoker.getArgs();
MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
Object parameterObject = queryArgs[PARAMETER_INDEX];
BoundSql boundSql = ms.getBoundSql(parameterObject);
String sql = boundSql.getSql();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
ObjectFactory objectFactory = new DefaultObjectFactory();
ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
MetaObject metaObject = parameterObject == null ? null : MetaObject
.forObject(parameterObject, objectFactory, objectWrapperFactory, reflectorFactory);
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
PropertyTokenizer prop = new PropertyTokenizer(propertyName);
if (parameterObject == null) {
value = null;
} else if (ms.getConfiguration().getTypeHandlerRegistry().hasTypeHandler(
parameterObject.getClass())) {
value = parameterObject;
} else if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
&& boundSql.hasAdditionalParameter(prop.getName())) {
value = boundSql.getAdditionalParameter(prop.getName());
if (value != null) {
value = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory).getValue(
propertyName.substring(prop.getName().length()));
}
} else {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
if (value != null) {
boolean valueIsString = value instanceof String;
if (valueIsString && value.toString().indexOf("$") > -1) {
value = ((String) value).replaceAll("\\$", "\\\\\\$");
}
sql = sql.replaceFirst("\\?", valueIsString ? "'" + value + "'" : value.toString());
} else {
sql = sql.replaceFirst("\\?", "null");
}
}
}
if (properties != null &&
"true".equals(properties.getProperty("printSql", "false"))) {
System.out.println(String.format("当前运行的SQL[%s]:,耗时:%s ms", ms.getId(),System.currentTimeMillis()-startTime));
System.out.println(String.format("\t\n%s", sql));
}
}
}
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties p) {
this.properties = p;
}
}
在mybatis-config.xml配置文件中配置该插件
<configuration>
<plugins>
<plugin interceptor="com.march.common.mybatis.plugin.MybatisSqlFormatPlugin">
<property name="printSql" value="true"></property>
</plugin>
</plugins>
</configuration>
效果如下图:
当前运行的SQL[com.march.mapper.UserDAO.insert]:,耗时:197 ms
insert into test_user (id, name, age
)
values (10, ‘test’, 15
)