Map<String,Class> mysqlTypeMapping = new HashMap<String, Class>();
{
mysqlTypeMapping.put("INT",Integer.class);
mysqlTypeMapping.put("VARCHAR",String.class);
mysqlTypeMapping.put("TINYINT",Integer.class);
mysqlTypeMapping.put("DATETIME",Date.class);
}
public List<T> listEntity(Sql sql){
sql.setCallback(Sqls.callback.entities());
sql.setEntity(dao().getEntity(getEntityClass()));
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
List<T> list = new ArrayList<T>();
Map<String,String> columns = new HashMap<String, String>();
ResultSetMetaData rsmd = rs.getMetaData();
for(int i=1;i<=rsmd.getColumnCount();i++){
columns.put(rsmd.getColumnLabel(i),rsmd.getColumnTypeName(i));
}
Entity<T> entity = dao().getEntity(getEntityClass());
Class clazz = entity.getType();
Method m = null;
while (rs.next()) {
Object obj = null;
try{
obj = clazz.newInstance();
for(Map.Entry<String,String> entry : columns.entrySet()){
String columnName = entry.getKey();
String columnType = entry.getValue();
String methodName = "set" + StringUtil.upperInitial(columnName);
m = clazz.getMethod(methodName,mysqlTypeMapping.get(columnType));
if(null != m){
m.invoke(obj,rs.getObject(columnName));
}
}
}catch (Exception e){
continue;
}
list.add((T) obj);
}
return list;
}
});
dao().execute(sql);
List<T> list = sql.getList(getEntityClass());
return list;
}
大概写了一个还没完善 先放出来吧 也仅仅兼容mysql 可以先这么用着 后面等项目做完了完善了再放出来吧