我正在研究從數據庫提取數據的小型Java庫。Java字段猜測
假設有一個通用 Java類:
公共MyClass類{
public int valueA;
public String valueB;
public Double valueC;
//getter and setter methods.
}
之後,我定義一個SQL查詢字符串,我得到的類泛型類MyClass
的。
String sql = "Select c.valueA, c.valueB, c.valueC from my_table c";
Class<MyClass> type = MyClass.class;
List<MyClass> results = extractData(sql, type)M
凡方法extractData
是像下面的方法:
public<T> List<T> extractData(String sql, Class<T> type){
//1)Do everything that is necessary to retrieve a ResultSet by the sql String.
//2)Then, for each row in the ResultSet
//2.1)Create a new instance of object of type T.
//2.2)Automatically matches the field name from the query
// with the field name of the specified class.
//2.3) add the object to the list to be returned.
//3) return the list of results of type T.
}
在實踐中,如果可能的話,我怎麼能實現步驟2.2?
我想這是可能的,因爲我使用這種方法進入PHP框架。
尋找'反思'。看起來你正在構建一個ORM映射器。看看現有的解決方案可能是值得的。它們並不像JPA/Hibernate那樣沉重,例如考慮ibatis或google。 – 2013-04-26 11:58:54
我會看看他們兩個!謝謝。 – 2013-04-26 11:59:56