Java中ResultSet資料自動填滿到類中&類執行個體填充PreparedStatement

來源:互聯網
上載者:User

需求:

(1)提取ResultSet中值到一個類中挺麻煩的,需要new一個變數,使用對應成員的Set方法賦值,能不能根據類的Class,直接提取出ResultSet中的資料,最後返回執行個體化的類?

(2)可以用PreparedStatement先行編譯帶變數的sql語句,在execute之前需要將變數值填充進去,一個一個設定太麻煩了,能不能提供一個類變數,使用類成員變數的值自動填滿PreparedStatement?

這樣的功能許多開源的架構可以實現,但是不想因為這麼一點點的需求去學習那麼龐大的一套架構,於是自己實現了一下,總結下自己的實現思路。

實現這套架構下面兩個問題是必須考慮到的:

資料庫表的欄位名稱可能和類的成員名稱不一樣。舉個例子資料庫表中經常有這樣的命名modify_time,在類中很少會使用底線,一般傾向於modifyTime這樣的命名方式。

先行編譯的sql語句中變數的順序和類中成員變數的順序可能會不同。

怎麼將類成員的值對應到先行編譯好的PreparedStatement中對應的變數上。

解決問題1和問題2的方法是為類成員提供別名,使用java中的Annotation(標註)機制,標註介面如下:

1 @Target(ElementType.FIELD)

2 @Retention(RetentionPolicy.RUNTIME)

3 public @interface Column {

4     String value();

5 }

標註的例子:

public class Work extends WorkAbstraction{            @Column(value = "coop_type")    private Integer coopType;            private String subtitle;    private String coworker;    private String tutor;            @Column(value = "create_time")    private String createTime;            @Column(value = "modify_time")    private String modifyTime;    private String content;}

在資料庫中有一個work表,有一個欄位名是modify_time,對應實體類Work中相應欄位被命名為modifyTime,使用@Column(value = "modify_time")為屬性modifyTime提供別名modify_time,對於沒有別名的屬性,預設其和ResultSet中的列名稱相同。

僅僅提供別名是不夠的,從ResultSet中可以擷取到查詢結果的列名稱欄位,此時如果想要將ResultSet中的資料填充到某個class中,還需要一個資料結構:

HashMap<Class<?>,HashMap<String,Field>> mappingPools;

HashMap的主鍵是資料類的Class,HashMap的Value還是一個Map,HashMap<String,Field>,該Map的主鍵是類中成員變數的別名,value是java反射類型Field,利用java反射機制就可以很容易通過Field向資料類填充資料。

接下來的問題就是已知class,怎麼解析出上面的HashMap結構,代碼如下:

public class BeanMappingPool {    private static Lock lock = new ReentrantLock();    private static HashMap<Class<?>,HashMap<String,Field>> mappingPools;    static{        mappingPools = new HashMap<Class<?>,HashMap<String,Field>>();    };    public static HashMap<String,Field> GetFieldsMap(Class<?> objClass){        if(mappingPools.get(objClass) != null)            return mappingPools.get(objClass);        lock.lock();        if(mappingPools.get(objClass) != null){            lock.unlock();            return mappingPools.get(objClass);        }        HashMap<String,Field> pools = new HashMap<String,Field>();        for(;objClass != Object.class; objClass = objClass.getSuperclass())            for(Field f: objClass.getDeclaredFields()){                f.setAccessible(true);                Column col = f.getAnnotation(Column.class);                if(col != null)                    pools.put(col.value(), f);                else                    pools.put(f.getName(), f);            }        mappingPools.put(objClass, pools);        lock.unlock();        return pools;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.