Mybatis插件实现动态修改SQL语句


功能描述:

通过使用Mybatis插件功能,拦截SQL并且进行动态修改处理

MybatisPlugin类

插件执行类

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

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();

        BoundSql boundSql = statementHandler.getBoundSql();
        String sql = boundSql.getSql();
        // 做一些处理SQL语句;
        String newSql = sql;
        ReflectionUtils.setFieldValue(boundSql, "sql", newSql);
        return invocation.proceed();

    }

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

    @Override
    public void setProperties(Properties properties) {
    }

}

ReflectionUtils类

public class ReflectionUtils {

    /**
     * 通过反射将object转map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> object2Map(Object obj) {
        Map<String, Object> map;
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            int fieldSize = fields.length;
            if (fieldSize > 0) {
                map = new HashMap<>(fieldSize);
                for (int i = 0; i < fieldSize; i++) {
                    Field field = fields[i];
                    makeAccessible(field);
                    map.put(field.getName(), field.get(obj));
                }
                return map;
            }
        } catch (IllegalAccessException e) {
        }
        map = new HashMap<>(16);
        return map;
    }

    /**
     * 直接设置对象属性值,忽视private/protected修饰符
     *
     * @param object
     * @param fieldName
     * @param value
     */
    public static void setFieldValue(final Object object, final String fieldName, final Object value) {
        Field field = getDeclaredField(object, fieldName);

        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
        }

        makeAccessible(field);

        try {
            field.set(object, value);
        } catch (IllegalAccessException e) {

        }
    }

    /**
     * 取对象的DeclaredField
     *
     * @param object
     * @param fieldName
     * @return
     */
    protected static Field getDeclaredField(final Object object, final String fieldName) {
        for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
            try {
                return superClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
            }
        }
        return null;
    }

    /**
     * 设置属性的访问权限
     *
     * @param field
     */
    protected static void makeAccessible(final Field field) {
        if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
            field.setAccessible(true);
        }
    }

}

MapperConfig配置类

项目使用配置类注入Mapper插件

@Configuration
@MapperScan({"com.test.dao"})
public class MapperConfig {

    @Bean
    public MybatisLogPlugin myPlugin() {

        return new MybatisPlugin();
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM