0

さて、私はこのメソッドを持っていますが、その一部を処理する方法がわからない場合は、コメントが感嘆符で始まる場所です:「// !!!! これは私に関係する部分です...」

public Person getPersonFromRS(ResultSet rs) throws SQLException, NoSuchMethodException,
    IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
  Person person = new Person();

  // getting the fiels and methods of the class Person
  Field[] fields = person.getClass().getDeclaredFields();
  Method[] methods = Person.class.getClass().getDeclaredMethods();
  String setter = "set";

  // just ignoring these fields of the class
  for (Field field : fields) {
    if (field.getName().equals("nul") || field.getName().equals("pairMarriages")
        || field.getName().equals("pairMarriage_Date") || field.getName().equals("pairBiography")) {
      continue;
    }

    // getting the value from resultSet as string
    String value = "";
    try {
      value = rs.getString(field.getName());
    } catch (Exception e) {
      System.err.println("Error with getting the column " + field.getName() + " from database");
      continue;
    }

    // if value is not null and the string inside not NULL, set the value to the person
    if (!(value == null))
      System.out.println("THE RETRIEVED VALUE: " + value);

    if ((!(value == null)) && !value.equals(nul)) {
      // methodName = setParameter
      String methodName = setter + field.getName();
      System.out.println("\nmethod Name: " + methodName);

      Method method = null;
      try {
        System.out.println("The field type is " + field.getType());
        method = person.getClass().getDeclaredMethod(methodName, field.getType());
      } catch (Exception e) {
        System.err.println(e);
        continue;
      }

      // !!!! this is the part that concerns me, what's the best way to handle this part in order
      // to avoid this flood for every type?
      // determining the type of the value to set in
      Type type = field.getType();
      if (field.getType() == String.class) {
        method.invoke(person, value);
      } else if (field.getType() == long.class) {
        method.invoke(person, Long.parseLong(value));
      } else if (field.getType() == int.class) {
        method.invoke(person, Integer.parseInt(value));
      } else if (field.getType() == PersonDate.class) {
        PersonDate date = new PersonDate(value);
        method.invoke(person, date);
      }
    }
  }
  return person;
}

このようにすべての引数タイプを処理せずに、それを行う最適な方法はありますか? これは私にとってオーバーヘッドのように見えるということですか?

4

2 に答える 2

1

から最初にマップを作成できますResultSet。これmapには、列名とそれに対応する値が含まれます。

Map<String, String> map = new HashMap<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
    map.put(metaData.getColumnName(column), rs.getString(column));
}
return map;

map次に、これをpopulateのインスタンスに使用できますPersonこれには、 Apache Commons BeanUtilsの一部であるクラスのpopulateメソッドを使用する必要があります。BeanUtils

Person person = new Person();
BeanUtils.populate(person, map);

このクラスには次のものがあります。

リフレクションを介して JavaBeans プロパティを設定するためのユーティリティ メソッド。

必要に応じて、必要なフィールドに入力するようにクエリを変更できます。

于 2012-05-11T16:21:08.967 に答える
1

switch ステートメントはよりクリーンになりますが、JDO、JPA、休止状態などの実際の ORM ソリューションを投入したい場合を除き、それを行う必要があります。あなたの懸念は何ですか?「オーバーヘッド」と言うときに CPU サイクルを意味する場合は、一連の if/else ステートメントよりも、実行しているすべてのリフレクションについてもっと心配する必要があります。あなたの懸念がメンテナンスである場合、そうです、それは多くのコードになるでしょう. とはいえ、12 個ほどの型しかマッピングしていない場合は、それほど大したことではありません。ここでは、既製の ORM ツールを使用する以外に近道はありません。

于 2012-05-11T16:13:36.020 に答える