例如,我们有一个Person对象:
private int id;
private String firstName;
private String lastName;
private String sex;
private int age;
private Address address;
//加上setter和getter
这是我的结果集
public class PersonResultSet implements ResultSetExtractor<Person>{
@Override
public Person extractData(ResultSet rs) throws SQLException, DataAccessException {
Person person = new Person();
person.setId(rs.getInt("id"));
person.setFirstName(rs.getString("firstName"));
person.setLastName(rs.getString("lastName"));
person.setSex(rs.getString("sex"));
person.setAge(rs.getInt("age"));
// How do i get the address from the table with the ID
return person;
}
}
我如何从人员表中的地址ID对应的地址表中获取该人的地址?
答案 0 :(得分:1)
您必须在查询中加入
select p.*, a.*
from person p inner join address a on (p.address_id = a.id)
where ...;
并且在您的extractData方法中,您将能够访问地址'colums
@Override
public Person extractData(ResultSet rs) throws SQLException, DataAccessException {
Person person = new Person();
person.setId(rs.getInt("id"));
person.setFirstName(rs.getString("firstName"));
person.setLastName(rs.getString("lastName"));
person.setSex(rs.getString("sex"));
person.setAge(rs.getInt("age"));
Address address = new Address();
// Make sets as your are doing with person.
person.setAddress(address);
return person;
}