[阅读: 647] 2006-11-24 07:18:21
RowMapper rowMapper1 = new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map mapOfColValues = createColumnMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
String key = getColumnKey(rsmd.getColumnName(i));
Object obj = getColumnValue(rs, i);
mapOfColValues.put(key, obj);
}
return mapOfColValues;
}
protected Map createColumnMap(int columnCount) {
return CollectionFactory
.createLinkedCaseInsensitiveMapIfPossible(columnCount);
}
protected String getColumnKey(String columnName) {
return columnName;
}
protected Object getColumnValue(ResultSet rs, int index)
throws SQLException {
return JdbcUtils.getResultSetValue(rs, index);
}
};
RowMapper rowMapper2 = new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Object[] objs = new Object[columnCount];
objs[0] = rs.getString("userID");
objs[1] = rs.getString("username");
objs[2] = rs.getString("password");
objs[3] = rs.getString("status");
return objs;
}
};
long startTime1 = System.currentTimeMillis();
List list = template.query("select *from T_user", rowMapper1);
if (list != null && list.size() > 0) {
System.out.println(list.size());
for (int m = 0; m < list.size(); m++) {
Map obj = (Map) list.get(m);
System.out.println(obj.get("userID") + "\t"
+ obj.get("userName") + "\t" + obj.get("password"));
}
}
long endTime1 = System.currentTimeMillis();
long runTime1 = endTime1- startTime1;
System.out.println("rowMapper1\t"+elapsedTimeAsString(runTime1) + " seconds");
long startTime2= System.currentTimeMillis();
List list2 = template.query("select *from T_user", rowMapper2);
System.out.println("the rowMapper2");
if (list2 != null && list2.size() > 0) {
for (int m = 0; m < list2.size(); m++) {
Object[] objs = new Object[4];
objs = (Object[]) list2.get(m);
System.out.println(objs[0] + "\t" + objs[1] + "\t" + objs[2]);
}
}
long endTime2 = System.currentTimeMillis();
long runTime2 = endTime2 - startTime2;
System.out.println("rowMapper2\t"+elapsedTimeAsString(runTime2) + " seconds");
long startTime1 = System.currentTimeMillis();
List list = hibernateTemplate.find("from User");
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
System.out.println(user.getUserName() + "\t"
+ user.getPassword() + "\t" + user.getUserID() + "\t"
+ user.getStatus());
}
}
long endTime1 = System.currentTimeMillis();
long runTime1 = endTime1 - startTime1;
System.out.println(list.size()+"条\t" + elapsedTimeAsString(runTime1)
+ " seconds");
}
采用hibernate
22条 1.078 seconds
采用rowMapper1
rowMapper1 0.203 seconds
采用rowMapper2
rowMapper2 0.047 seconds