Search in sources :

Example 36 with MapperMethod

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

the class SqlProviderTest method differentTypeAndValue.

@Test
void differentTypeAndValue() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("differentTypeAndValue");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(DeleteProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Cannot specify different class on 'value' and 'type' attribute of @DeleteProvider at the 'public abstract void org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorMapper.differentTypeAndValue()'."));
    }
}
Also used : DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) 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 37 with MapperMethod

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

the class MapperProxyAggent method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
        try {
            return method.invoke(this, args);
        } catch (Throwable t) {
            throw ExceptionUtil.unwrapThrowable(t);
        }
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    Pair<Boolean, Class> isModel = MapperProxyAggentProcess.checkIsModel(method);
    Object execute = null;
    if (isModel.getLeft()) {
        try {
            MapperProxyAggentProcess.markModel();
            execute = mapperMethod.execute(sqlSession, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperProxyAggentProcess.resetModel();
        }
        if (execute instanceof Collection || execute instanceof Map) {
            // resultType : model
            Collection result = Collections.EMPTY_LIST;
            if (execute instanceof Collection) {
                result = (Collection) execute;
            } else {
                result = new LinkedList<>();
                result.add(execute);
            }
            ArrayList models = ModelParseUtils.converterMap2Model(isModel.getRight(), new LinkedList<>(result));
            Pair<Boolean, Collection> collection = MapperProxyAggentProcess.isCollection(method);
            if (collection.getLeft()) {
                Collection right = collection.getRight();
                right.addAll(models);
                return right;
            }
            if (models.size() > 0) {
                return models.get(0);
            }
            Constructor constructor = MapperProxyAggentProcess.getRetrunClass(method).getConstructor();
            constructor.setAccessible(true);
            return constructor.newInstance();
        }
    } else {
        execute = mapperMethod.execute(sqlSession, args);
    }
    return execute;
}
Also used : Constructor(java.lang.reflect.Constructor) MapperMethod(org.apache.ibatis.binding.MapperMethod)

Example 38 with MapperMethod

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

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) 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 39 with MapperMethod

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

the class SqlProviderTest method methodNotFound.

@Test
void methodNotFound() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("methodNotFound");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. Method 'methodNotFound' not found in SqlProvider 'org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorSqlBuilder'."));
    }
}
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 40 with MapperMethod

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

the class SqlProviderTest method invokeNestedError.

@Test
void invokeNestedError() throws NoSuchMethodException {
    try {
        Class<?> mapperType = ErrorMapper.class;
        Method mapperMethod = mapperType.getMethod("invokeNestedError");
        new ProviderSqlSource(new Configuration(), mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod).getBoundSql(new Object());
        fail();
    } catch (BuilderException e) {
        assertTrue(e.getMessage().contains("Error invoking SqlProvider method 'public java.lang.String org.apache.ibatis.submitted.sqlprovider.SqlProviderTest$ErrorSqlBuilder.invokeNestedError()' with specify parameter 'class java.lang.Object'.  Cause: java.lang.UnsupportedOperationException: invokeNestedError"));
    }
}
Also used : 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)

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