JAVA涂鴉
          關于JAVA的點點滴滴
          posts - 50,  comments - 689,  trackbacks - 0
          hibernate3.2有個新功能叫做annotation,這個功能在ejb3.0就出現了。而且spring2.x版本也有這個功能,這個功能到底有什么作用,我們還是看看解釋是什么:
          在已經發布的JDK1.5(tiger)中增加新的特色叫 Annotation。Annotation提供一種機制,將程序的元素如:類,方法,屬性,參數,本地變量,包和元數據聯系起來。這樣編譯器可以將元數據存儲在Class文件中。這樣虛擬機和其它對象可以根據這些元數據來決定如何使用這些程序元素或改變它們的行為。
          spring的配置文件比較復雜,所以這次先使用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文件,因為不需要了,我們現在只需要在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;//碟數
              @Column(name="game_version")
              
          private String gameVersion;//版本
              @Column(name="game_media")
              
          private String gameMedia;//介質
              @Column(name="game_copyright")
              
          private String gameCopyright;//版權
              @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發揮作用的地方了,是不是很方便呢。

          現在可以將com.game.bean.hibernate整個文件夾都去掉了,因為我們在spring的applicationContext中進行定義了。
          修改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了,是不是很簡單呢。

          示例代碼

          效果:


          相關信息:
          Struts2.0+spring2.0+hibernate3.1 ACEGI應用示例

          struts2.0+spring2.0+hibernate3.1 web應用 示例代碼下載

          項目架構以及數據庫信息
          struts+spring+hibernate的web應用<一> 架構搭建

          更多信息



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

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



          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(35)

          隨筆檔案

          文章分類

          文章檔案

          好友的blog

          我的其他blog

          老婆的Blog

          搜索

          •  

          積分與排名

          • 積分 - 775366
          • 排名 - 56

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大英县| 苍溪县| 凉城县| 朔州市| 黄浦区| 永城市| 萨嘎县| 外汇| 阳信县| 枣阳市| 柳河县| 泽州县| 阳谷县| 营口市| 科尔| 蕲春县| 灯塔市| 东光县| 彭水| 榆中县| 通辽市| 大关县| 大安市| 绵阳市| 临城县| 廉江市| 景宁| 绍兴市| 潮安县| 平塘县| 应用必备| 九江市| 巩义市| 博爱县| 江门市| 舟山市| 罗定市| 昌邑市| 邵武市| 临海市| 克什克腾旗|