Beispiel #1
0
  /**
   * 将查询结构设置给对应的域(一对多)
   *
   * @param list
   * @param majorMethod
   * @param fieldName
   * @param childEntities
   * @param childClass
   * @param foreignKey
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  private void setListValue(
      List<T> list,
      Method majorMethod,
      String fieldName,
      List<Entity> childEntities,
      Class<Entity> childClass,
      String foreignKey)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Map<Object, List<Entity>> childListMap = new HashMap<Object, List<Entity>>();

    for (Entity childEntity : childEntities) {
      Method foreignKeyMethod =
          childClass.getDeclaredMethod("get".concat(StringUtils.firstToUpperCase(foreignKey)));
      Object id = foreignKeyMethod.invoke(childEntity);

      List<Entity> childList = childListMap.get(id);
      if (childList == null) {
        childList = new ArrayList<Entity>();
        childListMap.put(id, childList);
      }
      childList.add(childEntity);
    }

    String listMethodName = "set".concat(StringUtils.firstToUpperCase(fieldName));
    for (T entity : list) {
      Object id = majorMethod.invoke(entity);
      List<Entity> childLis = childListMap.get(id);

      Method listMethod = entityClass.getDeclaredMethod(listMethodName, List.class);
      listMethod.invoke(entity, childLis);
    }
  }
Beispiel #2
0
  /**
   * 将查询结构设置给对应的域(一对一/多对一)
   *
   * @param list
   * @param keyMethod
   * @param fieldName
   * @param childEntities
   * @param childClass
   * @param fileType
   * @param refKey
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  private void setSingleValue(
      List<T> list,
      Method keyMethod,
      String fieldName,
      List<Entity> childEntities,
      Class<Entity> childClass,
      Class<?> fileType,
      String refKey)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Map<Object, Entity> childMap = new HashMap<Object, Entity>();

    for (Entity childEntity : childEntities) {
      Method refKeyMethod =
          childClass.getDeclaredMethod("get".concat(StringUtils.firstToUpperCase(refKey)));
      Object id = refKeyMethod.invoke(childEntity);

      childMap.put(id, childEntity);
    }

    String fieldMethodName = "set".concat(StringUtils.firstToUpperCase(fieldName));
    for (T entity : list) {
      Object id = keyMethod.invoke(entity);
      Entity child = childMap.get(id);

      Method fieldMethod = entityClass.getDeclaredMethod(fieldMethodName, fileType);
      fieldMethod.invoke(entity, child);
    }
  }
Beispiel #3
0
  /**
   * 解析数据关联关系
   *
   * @param list
   * @param fieldName
   * @param keyMethod
   * @param table
   * @param refKey
   * @param ids
   * @throws NoSuchFieldException
   * @throws NoSuchMethodException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  private void parseReference(
      List<T> list,
      String fieldName,
      Method keyMethod,
      String table,
      String refKey,
      StringBuffer ids)
      throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException,
          InvocationTargetException {
    Coupler<Entity> childCoupler = childCouplers.get(fieldName);
    Class<Entity> childClass = childCoupler.getClazz();
    TreeDao<Entity> childDao = DaoFactory.getTreeDao(childClass);

    String childSql = buildChildQuerySql(table, refKey, ids, ordersMap, fieldName);

    if (entityClass == childClass) {
      int count = dao.count(childSql);
      if (count == 0) {
        return;
      }
    }

    List<Entity> childEntities = childDao.queryForTree(childSql);

    Class<?> fileType = entityClass.getDeclaredField(fieldName).getType();
    if (fileType == List.class) {
      setListValue(list, keyMethod, fieldName, childEntities, childClass, refKey);
    } else {
      setSingleValue(list, keyMethod, fieldName, childEntities, childClass, fileType, refKey);
    }
  }
Beispiel #4
0
  /**
   * 树形结构的数据查询
   *
   * @param sql
   * @param values
   * @return
   */
  @Override
  public List<T> queryForTree(String sql, Object... values) {
    List<T> list = dao.find(sql, values);
    if (list == null || list.isEmpty()) {
      return list;
    }

    if (childCouplers.isEmpty()) {
      return list;
    }

    try {
      Method majorMethod = entityClass.getDeclaredMethod(coupler.getMajorFieldGetterName());

      StringBuffer ids = new StringBuffer();
      int i = 0;
      for (Entity entity : list) {
        ids.append(majorMethod.invoke(entity));
        if (i++ < list.size() - 1) {
          ids.append(",");
        }
      }

      for (String fieldName : oneToOneMap.keySet()) {
        OneToOne oneToOne = oneToOneMap.get(fieldName);
        String table = oneToOne.table();
        String refKey = oneToOne.foreignKey();
        parseReference(list, fieldName, majorMethod, table, refKey, ids);
      }
      for (String fieldName : oneToManyMap.keySet()) {
        OneToMany oneToMany = oneToManyMap.get(fieldName);
        String table = oneToMany.table();
        String refKey = oneToMany.foreignKey();
        parseReference(list, fieldName, majorMethod, table, refKey, ids);
      }
      for (String fieldName : manyToOneMap.keySet()) {
        ManyToOne manyToOne = manyToOneMap.get(fieldName);
        String table = manyToOne.table();
        String refKey = manyToOne.referenceKey();
        Method keyMethod =
            entityClass.getDeclaredMethod("get".concat(StringUtils.firstToUpperCase(refKey)));
        StringBuffer refIds = new StringBuffer();
        int j = 0;
        for (Entity entity : list) {
          refIds.append(keyMethod.invoke(entity));
          if (j++ < list.size() - 1) {
            refIds.append(",");
          }
        }
        parseReference(list, fieldName, keyMethod, table, refKey, refIds);
      }
    } catch (NoSuchMethodException e) {
      log.error("SimpleTreeDao.queryForTree throws NoSuchMethodException", e);
    } catch (InvocationTargetException e) {
      log.error("SimpleTreeDao.queryForTree throws InvocationTargetException", e);
    } catch (IllegalAccessException e) {
      log.error("SimpleTreeDao.queryForTree throws IllegalAccessException", e);
    } catch (NoSuchFieldException e) {
      log.error("SimpleTreeDao.queryForTree throws NoSuchFieldException", e);
    }
    return list;
  }