两位地图制作者如下:
class Apple implements RowMapper<Apple> {
public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
Apple a = new Apple();
a.setName(rs.getString("Name"));
a.setName(rs.getString("Color"));
a.setDesc(rs.getString("Desc"));
}
}
class Apple implements RowMapper<Apple> {
public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
Apple a = new Apple();
a.setName(rs.getString("Name"));
a.setName(rs.getString("Color"));
a.setCost(rs.getString("Cost"));
}
}
}
我有两个Rowmappers返回相同的bean。如果第一个成本不存在,则结果集将不同,在第二种情况下,Desc将不会出现在结果集中。在这种情况下,我可以使用单个映射器,如下所示:
class Apple implements RowMapper<Apple> {
Private Boolean determine;
public Apple mapRow(ResultSet rs, int rowNum) throws SQLException {
Apple a = new Apple();
a.setName(rs.getString("Name"));
a.setName(rs.getString("Color"));
if(determine==true){
a.setCost(rs.getString("Cost"));
}else{
a.setDesc(rs.getString("Desc"));
}
}
}
}
在调用mapper时在构造函数中传递Boolean。如果不是最好的方法,可以实施吗。
注意:请忽略语法错误。