org.apache.ibatis.binding.BindingException Java Examples

The following examples show how to use org.apache.ibatis.binding.BindingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeneralExceptionsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
Example #2
Source File: PaginationMapperProxy.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
    if (isObjectMethod(method)) {
        return null;
    }
    final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
    if (Page.class.isAssignableFrom(method.getReturnType())) {
        // 分页处理
        return new PaginationMapperMethod(declaringInterface, method, sqlSession).execute(args);
    }
    // 原处理方式
    final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession.getConfiguration());
    final Object result = mapperMethod.execute(sqlSession, args);
    if (result == null && method.getReturnType().isPrimitive()) {
        throw new BindingException(
                "Mapper method '"
                        + method.getName()
                        + "' ("
                        + method.getDeclaringClass()
                        + ") attempted to return null from a method with a primitive return type ("
                        + method.getReturnType() + ").");
    }
    return result;
}
 
Example #3
Source File: PaginationMapperProxy.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
private Class<?> findDeclaringInterface(Object proxy, Method method) {
    Class<?> declaringInterface = null;
    for (Class<?> mapperFaces : proxy.getClass().getInterfaces()) {
        Method m = Reflections.getAccessibleMethod(mapperFaces,
                method.getName(),
                method.getParameterTypes());
        if (m != null) {
            declaringInterface = mapperFaces;
        }
    }
    if (declaringInterface == null) {
        throw new BindingException(
                "Could not find interface with the given method " + method);
    }
    return declaringInterface;
}
 
Example #4
Source File: MapperAnnotationBuilder.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterType, LanguageDriver languageDriver) {
  try {
    Class<? extends Annotation> sqlAnnotationType = getSqlAnnotationType(method);
    Class<? extends Annotation> sqlProviderAnnotationType = getSqlProviderAnnotationType(method);
    if (sqlAnnotationType != null) {
      if (sqlProviderAnnotationType != null) {
        throw new BindingException("You cannot supply both a static SQL and SqlProvider to method named " + method.getName());
      }
      Annotation sqlAnnotation = method.getAnnotation(sqlAnnotationType);
      final String[] strings = (String[]) sqlAnnotation.getClass().getMethod("value").invoke(sqlAnnotation);
      return buildSqlSourceFromStrings(strings, parameterType, languageDriver);
    } else if (sqlProviderAnnotationType != null) {
      Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
      return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation);
    }
    return null;
  } catch (Exception e) {
    throw new BuilderException("Could not find value method on SQL annotation.  Cause: " + e, e);
  }
}
 
Example #5
Source File: MapperAnnotationBuilder.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterType, LanguageDriver languageDriver) {
  try {
    Class<? extends Annotation> sqlAnnotationType = getSqlAnnotationType(method);
    Class<? extends Annotation> sqlProviderAnnotationType = getSqlProviderAnnotationType(method);
    if (sqlAnnotationType != null) {
      if (sqlProviderAnnotationType != null) {
        throw new BindingException("You cannot supply both a static SQL and SqlProvider to method named " + method.getName());
      }
      Annotation sqlAnnotation = method.getAnnotation(sqlAnnotationType);
      final String[] strings = (String[]) sqlAnnotation.getClass().getMethod("value").invoke(sqlAnnotation);
      return buildSqlSourceFromStrings(strings, parameterType, languageDriver);
    } else if (sqlProviderAnnotationType != null) {
      Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
      return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation);
    }
    return null;
  } catch (Exception e) {
    throw new BuilderException("Could not find value method on SQL annotation.  Cause: " + e, e);
  }
}
 
Example #6
Source File: GeneralExceptionsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
  Class<?>[] exceptionTypes = {
      BindingException.class,
      CacheException.class,
      DataSourceException.class,
      ExecutorException.class,
      LogException.class,
      ParsingException.class,
      BuilderException.class,
      PluginException.class,
      ReflectionException.class,
      PersistenceException.class,
      SqlSessionException.class,
      TransactionException.class,
      TypeException.class, 
      ScriptingException.class
  };
  for (Class<?> exceptionType : exceptionTypes) {
    testExceptionConstructors(exceptionType);
  }

}
 
Example #7
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #8
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithRowBounds.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    RowBounds bounds1 = new RowBounds(0, 1);
    RowBounds bounds2 = new RowBounds(0, 1);
    AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class);
    mapper.selectAuthor(101, bounds1, bounds2);
  } finally {
    sqlSession.close();
  }
}
 
Example #9
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #10
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected=BindingException.class)
public void shouldFailExecutingAnAnnotatedMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor2(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #11
Source File: DefaultSqlSession.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public V get(Object key) {
  if (!super.containsKey(key)) {
    throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + this.keySet());
  }
  return super.get(key);
}
 
Example #12
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithRowBounds.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    RowBounds bounds1 = new RowBounds(0, 1);
    RowBounds bounds2 = new RowBounds(0, 1);
    AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class);
    mapper.selectAuthor(101, bounds1, bounds2);
  } finally {
    sqlSession.close();
  }
}
 
Example #13
Source File: PaginationMapperRegistry.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
@Override
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    if (!hasMapper(type)) {
        throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
        return PaginationMapperProxy.newMapperProxy(type, sqlSession);
    } catch (Exception e) {
        throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
}
 
Example #14
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected=BindingException.class)
public void shouldFailExecutingAnAnnotatedMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor2(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #15
Source File: DefaultSqlSession.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public V get(Object key) {
  if (!super.containsKey(key)) {
    throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + this.keySet());
  }
  return super.get(key);
}
 
Example #16
Source File: PaginationMapperMethod.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 验证Statement
 */
private void validateStatement() {
    if (!config.hasStatement(commandName)) {
        throw new BindingException("Invalid bound statement (not found): " + commandName);
    }
    if (!config.hasStatement(commandCountName)) {
        throw new BindingException("Invalid bound statement (not found): " + commandCountName);
    }
}
 
Example #17
Source File: PaginationMapperMethod.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 设置当前的参数的类型信息
 */
private void setupCommandType() {
    MappedStatement ms = config.getMappedStatement(commandName);
    type = ms.getSqlCommandType();
    if (type != SqlCommandType.SELECT) {
        throw new BindingException("Unsupport execution method for: " + commandName);
    }
}