Example #1
0
 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;
 }
 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;
 }
Example #3
0
 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;
 }