コード例 #1
0
ファイル: MapperMethod.java プロジェクト: adaikiss/mybatis-3
 private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
   Map<K, V> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey(), rowBounds);
   } else {
     result = sqlSession.<K, V>selectMap(command.getName(), param, method.getMapKey());
   }
   return result;
 }
コード例 #2
0
ファイル: MapperMethod.java プロジェクト: adaikiss/mybatis-3
 private <T> Cursor<T> executeForCursor(SqlSession sqlSession, Object[] args) {
   Cursor<T> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<T>selectCursor(command.getName(), param, rowBounds);
   } else {
     result = sqlSession.<T>selectCursor(command.getName(), param);
   }
   return result;
 }
コード例 #3
0
ファイル: MapperMethod.java プロジェクト: adaikiss/mybatis-3
 private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
   MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
   if (void.class.equals(ms.getResultMaps().get(0).getType())) {
     throw new BindingException(
         "method "
             + command.getName()
             + " needs either a @ResultMap annotation, a @ResultType annotation,"
             + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
   }
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
   } else {
     sqlSession.select(command.getName(), param, method.extractResultHandler(args));
   }
 }
コード例 #4
0
ファイル: MapperMethod.java プロジェクト: adaikiss/mybatis-3
 private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
   List<E> result;
   Object param = method.convertArgsToSqlCommandParam(args);
   if (method.hasRowBounds()) {
     RowBounds rowBounds = method.extractRowBounds(args);
     result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
   } else {
     result = sqlSession.<E>selectList(command.getName(), param);
   }
   // issue #510 Collections & arrays support
   if (!method.getReturnType().isAssignableFrom(result.getClass())) {
     if (method.getReturnType().isArray()) {
       return convertToArray(result);
     } else {
       return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
     }
   }
   return result;
 }
コード例 #5
0
ファイル: MapperMethod.java プロジェクト: ChanBowei/mybatis-3
 public Object execute(SqlSession sqlSession, Object[] args) {
   Object result;
   switch (command.getType()) {
     case INSERT:
       {
         Object param = method.convertArgsToSqlCommandParam(args);
         result = rowCountResult(sqlSession.insert(command.getName(), param));
         break;
       }
     case UPDATE:
       {
         Object param = method.convertArgsToSqlCommandParam(args);
         result = rowCountResult(sqlSession.update(command.getName(), param));
         break;
       }
     case DELETE:
       {
         Object param = method.convertArgsToSqlCommandParam(args);
         result = rowCountResult(sqlSession.delete(command.getName(), param));
         break;
       }
     case SELECT:
       if (method.returnsVoid() && method.hasResultHandler()) {
         executeWithResultHandler(sqlSession, args);
         result = null;
       } else if (method.returnsMany()) {
         result = executeForMany(sqlSession, args);
       } else if (method.returnsMap()) {
         result = executeForMap(sqlSession, args);
       } else if (method.returnsCursor()) {
         result = executeForCursor(sqlSession, args);
       } else {
         Object param = method.convertArgsToSqlCommandParam(args);
         result = sqlSession.selectOne(command.getName(), param);
       }
       break;
     case FLUSH:
       result = sqlSession.flushStatements();
       break;
     default:
       throw new BindingException("Unknown execution method for: " + command.getName());
   }
   if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
     throw new BindingException(
         "Mapper method '"
             + command.getName()
             + " attempted to return null from a method with a primitive return type ("
             + method.getReturnType()
             + ").");
   }
   return result;
 }
コード例 #6
0
ファイル: MapperMethod.java プロジェクト: adaikiss/mybatis-3
 private Object rowCountResult(int rowCount) {
   final Object result;
   if (method.returnsVoid()) {
     result = null;
   } else if (Integer.class.equals(method.getReturnType())
       || Integer.TYPE.equals(method.getReturnType())) {
     result = Integer.valueOf(rowCount);
   } else if (Long.class.equals(method.getReturnType())
       || Long.TYPE.equals(method.getReturnType())) {
     result = Long.valueOf(rowCount);
   } else if (Boolean.class.equals(method.getReturnType())
       || Boolean.TYPE.equals(method.getReturnType())) {
     result = Boolean.valueOf(rowCount > 0);
   } else {
     throw new BindingException(
         "Mapper method '"
             + command.getName()
             + "' has an unsupported return type: "
             + method.getReturnType());
   }
   return result;
 }
コード例 #7
0
 public Object execute(SqlSession sqlSession, Object[] args) {
   Object result;
   if (SqlCommandType.INSERT == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.insert(command.getName(), param));
   } else if (SqlCommandType.UPDATE == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.update(command.getName(), param));
   } else if (SqlCommandType.DELETE == command.getType()) {
     Object param = method.convertArgsToSqlCommandParam(args);
     result = rowCountResult(sqlSession.delete(command.getName(), param));
   } else if (SqlCommandType.SELECT == command.getType()) {
     if (method.returnsVoid() && method.hasResultHandler()) {
       executeWithResultHandler(sqlSession, args);
       result = null;
     } else if (method.returnsMany()) {
       result = executeForMany(sqlSession, args);
     } else if (method.returnsMap()) {
       result = executeForMap(sqlSession, args);
     } else {
       Object param = method.convertArgsToSqlCommandParam(args);
       result = sqlSession.selectOne(command.getName(), param);
     }
   } else {
     throw new BindingException("Unknown execution method for: " + command.getName());
   }
   if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
     throw new BindingException(
         "Mapper method '"
             + command.getName()
             + " attempted to return null from a method with a primitive return type ("
             + method.getReturnType()
             + ").");
   }
   return result;
 }