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