Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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 按表从ResultSet中删除列_Java_Jdbc_Jdbi - Fatal编程技术网

Java 按表从ResultSet中删除列

Java 按表从ResultSet中删除列,java,jdbc,jdbi,Java,Jdbc,Jdbi,我正在使用JDBI查询数据库。我有两个通过外键连接的表,我的查询返回两个表的联接。要将返回的值转换为对象,我已经为我的对象实现了resultsetapper。查询中的ResultSet包含两个表的列:t1.id、t1.name、t2.id、t2.name。如何根据表名拆分表?我正在寻找以下代码: public class T1Object { private long id; private String name; private T2Object t2Object;

我正在使用JDBI查询数据库。我有两个通过外键连接的表,我的查询返回两个表的联接。要将返回的值转换为对象,我已经为我的对象实现了
resultsetapper
。查询中的
ResultSet
包含两个表的列:
t1.id、t1.name、t2.id、t2.name
。如何根据表名拆分表?我正在寻找以下代码:

public class T1Object {
    private long id;
    private String name;
    private T2Object t2Object;
}

public class T2Object {
    private long id;
    private String name;
}

public void map(ResultSet r) {
    String t1Name = "Table1 name";
    String t2Name = "Table2 name";

    t1ResultSet = getResultSetByTableName(r, t1Name); // looking for this function's implementation
    t2ResultSet = getResultSetByTableName(r, t2Name);

    // convert each result set to an object using it's mapper...
}
问题是
Table1
Table2
有一些列具有相同的名称,因此我考虑更改查询以为每个列返回不同的名称,并只解析整个
ResultSet
,但如果我有很多列或只对
Table2
进行更改,这将无法很好地扩展(我必须记住,不仅要更改
表2的映射器,还要返回此处进行更改)

我发现了这个,但它看起来太复杂了


如果您有任何想法,我们将不胜感激……

我建议您使用列名前缀作为可选构造函数参数来实现映射器:

class T1Mapper implements ResultSetMapper<T1Object> {
  public T1Mapper() { this(""); }
  public T1Mapper(String prefix) {
    this.prefix = prefix;
  }

  private final String prefix;

  T1Object map(int i, ResultSet rs, StatementContext ctx) throws SQLException {
    return new T1Object(rs.getInt(prefix + "id"),
                        rs.getString(prefix + "name"));
  }
}

// ditto for t2 mapper
因此:


我建议使用列名前缀作为可选构造函数参数来实现映射器:

class T1Mapper implements ResultSetMapper<T1Object> {
  public T1Mapper() { this(""); }
  public T1Mapper(String prefix) {
    this.prefix = prefix;
  }

  private final String prefix;

  T1Object map(int i, ResultSet rs, StatementContext ctx) throws SQLException {
    return new T1Object(rs.getInt(prefix + "id"),
                        rs.getString(prefix + "name"));
  }
}

// ditto for t2 mapper
因此:

@SqlQuery("select t1.id t1_id, t1.name t1_name, " +
          "t2.id t2_id, t2.name t2_name " +
          "from table1 t1 left join table2 t2 " +
          "on t1.id = t2.t1_id")
@RegisterBeanMapper(value = {T1Object.class, T2Object.class},
                    prefix = {"t1_", "t2_"})
@RegisterJoinRowMapper({T1Object.class, T2Object.class})
List<JoinRow> listJoins();
myDao.listJoins()
     .stream()
     .map(joinRow -> {
       T1Object t1 = joinRow.get(T1Object.class);
       T2Object t2 = joinRow.get(T2Object.class);

       // merge the individual entities in your join rows however you like
       t1.setT2(t2);
       return t1;
     })
     .collect(toList());