<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-42  評論-349  文章-4  trackbacks-0

    (殘夢追月原創(chuàng),轉(zhuǎn)載請注明)

    本文地址:http://m.tkk7.com/cmzy/archive/2008/09/11/228271.html?

    ?? 今天看SpringAPI的時候無意中發(fā)現(xiàn)了Spring2.5新增了一個RowMapper的實現(xiàn)類org.springframework.jdbc.core.BeanPropertyRowMapper,但是貌似Spring的refrence里面根本就沒提及到。Google了一下……貌似也莫得多少文檔。

    ??? Spring API Doc的說明如下:

    ?? RowMapper implementation that converts a row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.

    ?? Column values are mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties. The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.

    ?? Mapping is provided for fields in the target class for many common types, e.g.: String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long, float, Float, double, Double, BigDecimal, java.util.Date, etc.

    ?? To facilitate mapping between columns and fields that don't have matching names, try using column aliases in the SQL statement like "select fname as first_name from customer".

    ?? Please note that this class is designed to provide convenience rather than high performance. For best performance consider using a custom RowMapper.


    ?? 也就說,它可以把ResultSet和實體類的字段進行實現(xiàn)自動映射。

    ?? 一個具體的例子如下:

    ?? 假如有這樣一個表,SQL-Server2000的建表腳本如下:

    ?? 為此,我們編寫一個對應(yīng)的實體類admin,它是一個標(biāo)準(zhǔn)的javaBean,代碼如下:

    ?? 以前,在相應(yīng)的AdminDAO中,我們以前是這么做滴,看起來很麻煩,如果一個表的字段很多的話,就要人命了,我們必須不停的set、get:

    ?? 可見,我們必須的手工對ResultSet和Admin進行映射。而現(xiàn)在,我們只是需要這樣:

    ??? 呵呵,只是一句話就完全搞定了……Sprin會為我們自動映射……顯然這樣比以前方便多了。我們還可以把它用在其它任何使用RowMapper的場合……畢竟它繼承自RowMapper……

    ??? 需要注意的是:BeanPropertyRowMapper是根據(jù)字段名和實體類中的標(biāo)準(zhǔn)Setter方法進行映射滴。也就是說,我們需要使表中的字段名和實體類的成員變量名稱一致。





    By:殘夢追月
    posted on 2008-09-11 09:33 殘夢追月 閱讀(6182) 評論(8)  編輯  收藏 所屬分類: Spring

    評論:
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2008-09-11 13:18 | super2
    還是很生硬  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2008-09-11 18:17 | 殘夢追月
    @super2
    呵呵。我不是翻譯滴……只是介紹了哈它的用法……我哪個翻字用錯了。  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2008-09-12 10:36 |
    這個查詢太簡單了吧?要是只查某部分字段或復(fù)合查詢(如count(XX)、sun(XX))該怎么用?還能自動匹配嗎?  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2008-09-12 13:40 | 殘夢追月
    @巫
    這個只是個例子嘛……你可以用別名啊……
    select Count(*) as n form table  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2009-05-27 14:01 | liyaxi
    多謝兄弟 ,可是我在spring 2.0是已經(jīng)發(fā)現(xiàn)了,  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2010-04-13 09:24 | dinstone
    @liyaxi
    你太牛逼了,人家V2.5才出現(xiàn),你就在2.0看見了??
    /*
    * @author Thomas Risberg
    * @author Juergen Hoeller
    * @since 2.5
    */
    public class BeanPropertyRowMapper implements RowMapper

      回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。 2010-05-16 10:00 | chrislee
    這東西應(yīng)該不是在所有情況下都能通用的!
    如果在多表鏈接的時候,兩個表的字段中都用id,或者name的列名。
    映射的時候肯定就會亂掉。
    最近寫.net的東西的時候,就想到這樣的問題。寫了個通用的,需要在每個類中加上輔助的信息,以防止多表鏈接的時候這樣列名沖突的情況  回復(fù)  更多評論
      
    # re: 發(fā)現(xiàn)了Spring2.5里的一個新東西, BeanPropertyRowMapper類。[未登錄] 2012-03-12 15:18 | rover
    如果有下劃線,也會自動被轉(zhuǎn)化成camel命名法....  回復(fù)  更多評論
      
    主站蜘蛛池模板: 亚洲毛片基地日韩毛片基地| 亚洲国产免费综合| 亚洲电影一区二区| 国产一区二区三区免费| 亚洲第一AAAAA片| 午夜不卡久久精品无码免费| 亚洲欧洲日韩不卡| 18女人水真多免费高清毛片| 亚洲免费视频网址| 成全影视免费观看大全二| 亚洲JLZZJLZZ少妇| 亚洲AⅤ永久无码精品AA| 国产va免费观看| 亚洲国产综合91精品麻豆| 最近2019中文字幕免费大全5| 亚洲毛片在线免费观看| 免费看美女让人桶尿口| 国产精品亚洲一区二区三区| 亚洲毛片不卡av在线播放一区| A级毛片高清免费视频在线播放| 久久久久久亚洲AV无码专区| 91情侣在线精品国产免费| 羞羞视频免费观看| 久久久久亚洲精品成人网小说| 国产大片免费网站不卡美女| 亚洲丶国产丶欧美一区二区三区| 亚洲五月午夜免费在线视频| 国内精品免费在线观看| 亚洲一线产区二线产区区| JLZZJLZZ亚洲乱熟无码| 84pao国产成视频免费播放| 日韩欧美亚洲中文乱码| 亚洲色精品88色婷婷七月丁香| 日本免费xxxx| 一二三区免费视频| 久久亚洲精品成人无码网站| 国产伦精品一区二区三区免费下载| 久久久久免费视频| 亚洲视频无码高清在线| 久久亚洲国产精品五月天婷| 2020久久精品国产免费|