JAVA涂鴉
          關(guān)于JAVA的點點滴滴
          posts - 50,  comments - 689,  trackbacks - 0
          hibernate3.2有個新功能叫做annotation,這個功能在ejb3.0就出現(xiàn)了。而且spring2.x版本也有這個功能,這個功能到底有什么作用,我們還是看看解釋是什么:
          在已經(jīng)發(fā)布的JDK1.5(tiger)中增加新的特色叫 Annotation。Annotation提供一種機制,將程序的元素如:類,方法,屬性,參數(shù),本地變量,包和元數(shù)據(jù)聯(lián)系起來。這樣編譯器可以將元數(shù)據(jù)存儲在Class文件中。這樣虛擬機和其它對象可以根據(jù)這些元數(shù)據(jù)來決定如何使用這些程序元素或改變它們的行為。
          spring的配置文件比較復(fù)雜,所以這次先使用hibernate的annotation功能。

          項目所需包:



          先去掉我先前項目中的hibernate3.1,添加hibernate3.2,然后再下載hibernate-annotation,往lib文件夾中添加hibernate-annotations-3.3.0.jar,hibernate-commons-annotations.jar和ejb3-persistence.jar 。這樣就可以使用hibernate的annotation了。

          去掉products.hbm.xml文件,因為不需要了,我們現(xiàn)在只需要在products這個類中定義就可以了。代碼如下:
          package com.game.products.model;

          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.Table;

          import org.hibernate.annotations.GenericGenerator;


          @Entity
          @Table(name
          ="products")
          public class Products {
              
          //    Fields 
              @Id
              @GeneratedValue(generator 
          = "c-assigned")
              @GenericGenerator(name 
          = "c-assigned", strategy = "assigned")
              @Column(name
          ="game_id")
              
          private String gameId;//編號
              @Column(name="game_name_cn")
              
          private String gameNameCn;//中文名稱
              @Column(name="game_name_en")
              
          private String gameNameEn;//英文名稱
              @Column(name="game_capacity")
              
          private String gameCapacity;//碟數(shù)
              @Column(name="game_version")
              
          private String gameVersion;//版本
              @Column(name="game_media")
              
          private String gameMedia;//介質(zhì)
              @Column(name="game_copyright")
              
          private String gameCopyright;//版權(quán)
              @Column(name="game_price")
              
          private String gamePrice;//價格
              @Column(name="game_content")
              
          private String gameContent;//攻略
              
              
          //    Constructors
              public Products(){}
              
              
          //    Property accessors
              
              
          public String getGameCapacity() {
                  
          return gameCapacity;
              }

              
          public void setGameCapacity(String gameCapacity) {
                  
          this.gameCapacity = gameCapacity;
              }

              
              
          public String getGameId() {
                  
          return gameId;
              }

              
          public void setGameId(String gameId) {
                  
          this.gameId = gameId;
              }
              
              
              
          public String getGameNameCn() {
                  
          return gameNameCn;
              }

              
          public void setGameNameCn(String gameNameCn) {
                  
          this.gameNameCn = gameNameCn;
              }

              
              
          public String getGameNameEn() {
                  
          return gameNameEn;
              }

              
          public void setGameNameEn(String gameNameEn) {
                  
          this.gameNameEn = gameNameEn;
              }

              
              
          public String getGameVersion() {
                  
          return gameVersion;
              }

              
          public void setGameVersion(String gameVersion) {
                  
          this.gameVersion = gameVersion;
              }

              
              
          public String getGameMedia() {
                  
          return gameMedia;
              }

              
          public void setGameMedia(String gameMedia) {
                  
          this.gameMedia = gameMedia;
              }

              
              
          public String getGameCopyright() {
                  
          return gameCopyright;
              }

              
          public void setGameCopyright(String gameCopyright) {
                  
          this.gameCopyright = gameCopyright;
              }

              
              
          public String getGameContent() {
                  
          return gameContent;
              }

              
          public void setGameContent(String gameContent) {
                  
          this.gameContent = gameContent;
              }

              
              
          public String getGamePrice() {
                  
          return gamePrice;
              }

              
          public void setGamePrice(String gamePrice) {
                  
          this.gamePrice = gamePrice;
              }

          }
           
          注意類中的@符號沒有,這就是annotation發(fā)揮作用的地方了,是不是很方便呢。

          現(xiàn)在可以將com.game.bean.hibernate整個文件夾都去掉了,因為我們在spring的applicationContext中進(jìn)行定義了。
          修改applicationContext中的SessionFactory ,示例如下:
          <!-- SessionFactory -->
              
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="dataSource" ref="dataSource"/>
                  
          <property name="hibernateProperties">
                      
          <props>
                           
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                           
          <prop key="hibernate.hbm2ddl.auto">none</prop>
                      
          </props>
                  
          </property>
                  
          <property name="annotatedClasses">
                      
          <list>
                          
          <value>com.game.products.model.Products</value>
                      
          </list>
                  
          </property>
                      
              
          </bean>

          至此,我們就可以使用hibernate的annotation了,是不是很簡單呢。

          示例代碼

          效果:


          相關(guān)信息:
          Struts2.0+spring2.0+hibernate3.1 ACEGI應(yīng)用示例

          struts2.0+spring2.0+hibernate3.1 web應(yīng)用 示例代碼下載

          項目架構(gòu)以及數(shù)據(jù)庫信息
          struts+spring+hibernate的web應(yīng)用<一> 架構(gòu)搭建

          更多信息



          posted on 2007-05-04 09:19 千山鳥飛絕 閱讀(10676) 評論(13)  編輯  收藏 所屬分類: Web開發(fā)

          FeedBack:
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2007-05-23 18:47 | 小a
          兄弟把 lib放上啊
            回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例[未登錄]
          2007-08-04 11:48 | DuDu
          能否發(fā)給我lib中的包(lfqrl@163.com),或者發(fā)個鏈接下載也行啊,急需中......
          先謝了!  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2007-08-21 19:12 | winie
          能不能給份文檔``````````  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例[未登錄]
          2007-09-26 11:13 | 冰點
          Struts2和Spring2沒有用Annotation么?  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2007-10-01 17:42 | dfsdf
          垃圾  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2007-10-25 08:49 | wt
          兄弟能不能給傳個完整的程序啊.
          wanliyun1110@163.com  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2007-10-26 10:18 | 千山鳥飛絕
          @wt
          代碼是完整的,只是lib包太大了,blogjava沒有給那么大的空間。
            回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2008-04-14 13:09 | zan
          哥們把LIB給下吧 ,發(fā)到cheerzan@yahoo.com.cn。謝謝  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2008-06-30 17:25 | cbywxy
          有沒有源碼啊。共享一下  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2008-08-25 16:01 | buaa207
          哥們,項目所需包,我這邊也看不到鏈接,有l(wèi)ib包,能發(fā)一個?我的郵箱是:liqiangq@126.com  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2008-08-27 11:46 | avanry
          給我也發(fā)一個吧,多謝。avanry@126.com  回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例
          2008-09-11 23:20 | tt
          哥們,項目所需包,我這邊也看不到鏈接,有l(wèi)ib包,能發(fā)一個?我的郵箱是:tgm923@163.com   回復(fù)  更多評論
            
          # re: Struts2.0+spring2.0+hibernate3.2 Annotation應(yīng)用示例[未登錄]
          2012-04-27 15:36 | 飛飛
          181290200 Struts2.0+spring2.0+hibernate3.2 交流群  回復(fù)  更多評論
            
          正在閱讀:



          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(35)

          隨筆檔案

          文章分類

          文章檔案

          好友的blog

          我的其他blog

          老婆的Blog

          搜索

          •  

          積分與排名

          • 積分 - 775408
          • 排名 - 56

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 孝昌县| 渝北区| 苍南县| 宝山区| 正阳县| 霸州市| 武宣县| 肇州县| 丹寨县| 湖州市| 建水县| 呼图壁县| 牙克石市| 虞城县| 永胜县| 茌平县| 双牌县| 汝南县| 夏邑县| 芮城县| 军事| 宣化县| 科技| 樟树市| 玉屏| 普兰县| 舒城县| 靖州| 比如县| 汽车| 原阳县| 澳门| 清河县| 高碑店市| 永州市| 西畴县| 普兰店市| 安宁市| 宁陕县| 安西县| 太谷县|