Search in sources :

Example 16 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project mybatis-3 by mybatis.

the class SqlProviderTest method omitType.

@Test
void omitType() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("omitType");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Please specify either 'value' or 'type' attribute of @SelectProvider at the 'public abstract void org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorMapper.omitType()'."));
    }
}
Also used : SelectProvider(org.apache.ibatis.annotations.SelectProvider) BuilderException(org.apache.ibatis.builder.BuilderException) Configuration(org.apache.ibatis.session.Configuration) Method(java.lang.reflect.Method) MapperMethod(org.apache.ibatis.binding.MapperMethod) ProviderSqlSource(org.apache.ibatis.builder.annotation.ProviderSqlSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 17 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project Mybatis-ModelHelper by caomingjie-code.

the class MapperProxyAggent method cachedMapperMethod.

private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
        mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
        methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
}
Also used : MapperMethod(org.apache.ibatis.binding.MapperMethod)

Example 18 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project spring-boot-quick by vector4wang.

the class CryptParamInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    ParameterHandler parameterHandler = (ParameterHandler) invocation.getTarget();
    MetaObject metaObject = SystemMetaObject.forObject(parameterHandler);
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue(MAPPEDSTATEMENT);
    log.info("mappedStatement.getId(): {}", mappedStatement.getId());
    Object params = metaObject.getValue("parameterObject");
    if (Objects.isNull(params)) {
        return invocation.proceed();
    }
    /**
     * params 单个参数 insert(user)     Object
     * params 集合参数 insert(list<user>)  MapperMethod$ParamMap
     * https://github.com/miaoxinwei/mybatis-crypt/blob/master/src/main/java/org/apache/ibatis/plugin/CryptInterceptor.java
     */
    log.info("params.getClass().getTypeName(): {}", params.getClass().getTypeName());
    /**
     * MapperMethod.ParamMap
     * 参数使用@param,程序会自动增加对应的paramx
     */
    if (params instanceof MapperMethod.ParamMap) {
        MapperMethod.ParamMap<Object> paramMap = (MapperMethod.ParamMap<Object>) params;
        for (Map.Entry<String, Object> paramObj : paramMap.entrySet()) {
            Object paramValue = paramObj.getValue();
            if (CryptInterceptorUtil.isNotCrypt(paramValue) || paramObj.getKey().contains(GENERIC_NAME_PREFIX)) {
                continue;
            }
            log.info("paramValue.getClass().getTypeName(): {}", paramValue.getClass().getTypeName());
            // 集合类型的参数
            if (paramValue instanceof Collection) {
                listEntityCrypt((Collection) paramValue);
                continue;
            }
            // 对象类型的参数
            // entityEncrypt(paramObj);
            entityEncrypt(paramValue);
        }
    } else if (params instanceof Map) {
        return invocation.proceed();
    } else {
        // 走到这里一般代表方法中只有一个参数,并且米有添加@param注解
        log.warn("请检查方法中参数的写法,是否有加@param!!! 方法名为: {}", mappedStatement.getId());
        entityEncrypt(params);
    }
    return invocation.proceed();
}
Also used : ParameterHandler(org.apache.ibatis.executor.parameter.ParameterHandler) MapperMethod(org.apache.ibatis.binding.MapperMethod) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Example 19 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project spring-boot-quick by vector4wang.

the class CryptWriteInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue(MAPPEDSTATEMENT);
    log.info("mappedStatement.getId(): {}", mappedStatement.getId());
    SqlCommandType commandType = mappedStatement.getSqlCommandType();
    /**
     * 只有insert和update的才做处理
     */
    if (!CryptInterceptorUtil.isWriteCmd(commandType)) {
        return invocation.proceed();
    }
    BoundSql boundSql = (BoundSql) metaObject.getValue(BOUND_SQL);
    Object params = boundSql.getParameterObject();
    /**
     * params 单个参数 insert(user)     Object
     * params 集合参数 insert(list<user>)  MapperMethod$ParamMap
     * https://github.com/miaoxinwei/mybatis-crypt/blob/master/src/main/java/org/apache/ibatis/plugin/CryptInterceptor.java
     */
    log.info("params.getClass().getTypeName(): {}", params.getClass().getTypeName());
    /**
     * MapperMethod.ParamMap
     * 参数使用@param,程序会自动增加对应的paramx
     */
    if (params instanceof MapperMethod.ParamMap) {
        MapperMethod.ParamMap<Object> paramMap = (MapperMethod.ParamMap<Object>) params;
        for (Map.Entry<String, Object> paramObj : paramMap.entrySet()) {
            Object paramValue = paramObj.getValue();
            if (CryptInterceptorUtil.isNotCrypt(paramValue) || paramObj.getKey().contains(GENERIC_NAME_PREFIX)) {
                continue;
            }
            log.info("paramValue.getClass().getTypeName(): {}", paramValue.getClass().getTypeName());
            // 集合类型的参数
            if (paramValue instanceof Collection) {
                listEntityCrypt((Collection) paramValue);
                continue;
            }
            // 对象类型的参数
            // entityEncrypt(paramObj);
            entityEncrypt(paramValue);
        }
    } else if (params instanceof Map) {
        return invocation.proceed();
    } else {
        // 走到这里一般代表方法中只有一个参数,并且米有添加@param注解
        log.warn("请检查方法中参数的写法,是否有加@param!!! 方法名为: {}", mappedStatement.getId());
        entityEncrypt(params);
    }
    return invocation.proceed();
}
Also used : SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) MapperMethod(org.apache.ibatis.binding.MapperMethod) BoundSql(org.apache.ibatis.mapping.BoundSql) SqlCommandType(org.apache.ibatis.mapping.SqlCommandType) StatementHandler(org.apache.ibatis.executor.statement.StatementHandler) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Example 20 with MapperMethod

use of org.apache.ibatis.binding.MapperMethod in project javaBook-src by huifer.

the class SqlProviderTest method keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation.

@Test
@SuppressWarnings("deprecation")
void keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation() throws NoSuchMethodException {
    Class<?> mapperType = StaticMethodSqlProviderMapper.class;
    Method mapperMethod = mapperType.getMethod("noArgument");
    ProviderSqlSource sqlSource = new ProviderSqlSource(new Configuration(), (Object) mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
    assertEquals("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS", sqlSource.getBoundSql(null).getSql());
}
Also used : Configuration(org.apache.ibatis.session.Configuration) MapperMethod(org.apache.ibatis.binding.MapperMethod) Method(java.lang.reflect.Method) ProviderSqlSource(org.apache.ibatis.builder.annotation.ProviderSqlSource) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Aggregations

MapperMethod (org.apache.ibatis.binding.MapperMethod)41 Method (java.lang.reflect.Method)36 BaseDataTest (org.apache.ibatis.BaseDataTest)35 ProviderSqlSource (org.apache.ibatis.builder.annotation.ProviderSqlSource)35 Configuration (org.apache.ibatis.session.Configuration)35 Test (org.junit.jupiter.api.Test)35 BuilderException (org.apache.ibatis.builder.BuilderException)30 SelectProvider (org.apache.ibatis.annotations.SelectProvider)12 DeleteProvider (org.apache.ibatis.annotations.DeleteProvider)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)2 Constructor (java.lang.reflect.Constructor)1 ParameterHandler (org.apache.ibatis.executor.parameter.ParameterHandler)1 StatementHandler (org.apache.ibatis.executor.statement.StatementHandler)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 SqlCommandType (org.apache.ibatis.mapping.SqlCommandType)1 SqlSession (org.apache.ibatis.session.SqlSession)1