MyBatis 插件原理与自定义插件

MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强 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
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值