希望这是一个非常简单的问题。如何将类传递给抽象方法?
我的抽象方法如下:
public abstract class IDataList {
public LinkedList<IThing> getRows(IThing thing, String sql, List<Object> vals) throws Exception {
LinkedList<IThing> list = new LinkedList<>();
List<LinkedHashMap<String, Object>> rows = db.executeSelect(sql, vals);
for (HashMap<String, Object> row : rows) {
list.add(new thing(row));
}
rowCount = (long) getDb().executeScalar("SELECT FOUND_ROWS()");
return list;
}
}
由具体类继承:
public class DataList extends IDataList {
}
IThing目前是一个空的抽象类,它由Thing扩展,例如:
public class Thing extends IThing {
private long uid;
private String name;
public Thing(HashMap<String, Object> row) {
this.uid = (long) row.get("uid");
this.name = (String)row.get("name");
}
}
我希望能够将Thing传递给具体的IDataList类,例如:
IDataList dataList = new DataList();
dataList.getRows(Thing, "select something", new ArrayList<>())
答案 0 :(得分:0)
如果您想将Thing
类用作Row-&gt; Object mapper,最好使用org.springframework.jdbc.core.RowMapper
public class CustomerRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Customer customer = new Customer();
customer.setCustId(rs.getInt("CUST_ID"));
customer.setName(rs.getString("NAME"));
customer.setAge(rs.getInt("AGE"));
return customer;
}
}
public Customer findByCustomerId(int custId) {
String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
Customer customer = (Customer)getJdbcTemplate().queryForObject(sql, new Object[] { custId }, new CustomerRowMapper());
return customer;
}