Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有一种简单的方法来整合RowMapper的相同代码?_Java_Sql_Jdbc_Row - Fatal编程技术网

Java 是否有一种简单的方法来整合RowMapper的相同代码?

Java 是否有一种简单的方法来整合RowMapper的相同代码?,java,sql,jdbc,row,Java,Sql,Jdbc,Row,我正在使用JDBC,我的许多类都有一个内部行映射器类,如下所示: public class Foo { class AppleRows implements RowMapper<Apple> { public Apple mapRow(ResultSet rs, int rowNum) throws SQLException { Apple a = new Apple(); a.setName(rs.getString("Name"));

我正在使用JDBC,我的许多类都有一个内部行映射器类,如下所示:

public class Foo {
  class AppleRows implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new Apple();
      a.setName(rs.getString("Name"));
    }
  }

  class AppleRowsJoinedWithSomethingElse implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new Apple();   
      a.setName(rs.getString("Name"));
      a.setSomethingElse(rs.getString("SomethingElse"));
    } 
  }
}
公共类Foo{
类AppleRows实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=新苹果();
a、 setName(rs.getString(“Name”);
}
}
类AppleRowJoinedwithSomethingelse实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=新苹果();
a、 setName(rs.getString(“Name”);
a、 setSomethingElse(rs.getString(“SomethingElse”);
} 
}
}
在上面的示例中,
a.setName(rs.getString(“Name”))
行被复制。这只是一个示例,但在我的实际代码中有10多个类似的字段。我想知道有没有更好的办法


注意:我需要使用不同的映射器,因为我在某些地方使用它们,在那里我将结果与另一个表连接(获取更多字段)

您可以
扩展
+使用
super.mapRow()

public class Foo {
  class AppleRows implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new Apple();
      a.setName(rs.getString("Name"));
      return a;
    }
  }

  class AppleRowsJoinedWithSomethingElse extends AppleRows {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = super.mapRow(rs, rowNum);
      a.setSomethingElse(rs.getString("SomethingElse"));
      return a;
    } 
  }
}
公共类Foo{
类AppleRows实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=新苹果();
a、 setName(rs.getString(“Name”);
返回a;
}
}
类AppleRowsJoinedWithSomethingElse扩展了AppleRows{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=super.mapRow(rs,rowNum);
a、 setSomethingElse(rs.getString(“SomethingElse”);
返回a;
} 
}
}
或者,如果您不喜欢使用继承作为代码重用的机制,请简单地委托:

public class Foo {
  class AppleRows implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new Apple();
      a.setName(rs.getString("Name"));
      return a;
    }
  }

  class AppleRowsJoinedWithSomethingElse implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new AppleRows().mapRow(rs, rowNum);
      a.setSomethingElse(rs.getString("SomethingElse"));
      return a;
    } 
  }
}
公共类Foo{
类AppleRows实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=新苹果();
a、 setName(rs.getString(“Name”);
返回a;
}
}
类AppleRowJoinedwithSomethingelse实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
Apple a=新的AppleRows().mapRow(rs,rowNum);
a、 setSomethingElse(rs.getString(“SomethingElse”);
返回a;
} 
}
}

一种可能的方法是使用继承,特别是如果您可以将内部类设置为
静态
嵌套类,如下所示:

public class Foo {
  static class AppleRows implements RowMapper<Apple> {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = new Apple();
      a.setName(rs.getString("Name"));
      return a;
    }
  }

  static class AppleRowsJoinedWithSomethingElse extends AppleRows {
    public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
      Apple a = super.mapRow(rs, rowNum);
      a.setSomethingElse(rs.getString("SomethingElse"));
      return a;
    } 
  }
}
公共类Foo{
静态类AppleRows实现行映射器{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=新苹果();
a、 setName(rs.getString(“Name”);
返回a;
}
}
静态类AppleRowsJoinedWithSomethingElse扩展了AppleRows{
公共Apple mapRow(结果集rs,int rowNum)抛出SQLException{
苹果a=super.mapRow(rs,rowNum);
a、 setSomethingElse(rs.getString(“SomethingElse”);
返回a;
} 
}
}

我在这里使用
静态
嵌套类的原因是,在未使用的非
静态
嵌套类中(至少在当前实现中)存在对包含类的实例的隐式引用。

每个映射器映射一个行定义。我不会把这称为违反DRY

当在
AppleRowsJoinedWithSomethingElse
的基础视图中更改单个列名时,您只需在一个位置更改它


如果您使用公共代码重构此视图,那么如果提供
AppleRowsJoinedWithSomethingElse
的视图发生更改,但底层
AppleRows
的视图没有更改,那么您将处于一个奇怪的位置。

没有必要扩展以共享公共功能。我只是在输入这个,所以它实际上可能无法编译

public class CommonBlammy
{
    private CommonBlammy() { } // private constructor to prevent instantiation.

    public static Apple mapRow(final ResultSet resultSet)
    throws SQLException
    {
        Apple returnValue = new Apple();

        returnValue.setName(resultSet.getString("Name"));

        return returnValue;
    }
}

public class AppleRows implements RowMapper
{
    public Apple mapRow(
        final ResultSet resultSet,
        final int rowNumber)
        throws SQLException
    {
      return CommonBlammy.mapRow(ResultSet resultSet);
    }
}

public class AppleRowsJoinedWithSomethingElse implements RowMapper
{
    public Apple mapRow(
        final ResultSet resultSet,
        final int rowNumber)
    throws SQLException
    {
        Apple returnValue;

        returnValue = CommonBlammy.mapRow(ResultSet resultSet);
        returnValue.setSomethingElse(rs.getString("SomethingElse"));

        return returnValue;
    } 
}