1. 程式人生 > >使用註解方式進行spring和hibernate整合

使用註解方式進行spring和hibernate整合

整合spring和hibernate需要五個要素,分別包括持久化的類, 資料來源,SessionFactory, TransactionManager和持久化操作的DAO類。

持久化類:

  1. @Entity
  2. publicclass Spitter {  
  3.     privatelong id;  
  4.     private String userName, passWord, fullName;  
  5.     public Spitter(long id, String n, String p, String f){  
  6.         this.id = id;  
  7.         this.userName = n;  
  8.         this.passWord = p;  
  9.         this.fullName = f;  
  10.     }  
  11.     public Spitter(){}  
  12.     publicvoid setId(long id){  
  13.         this.id = id;  
  14.     }  
  15.     @Id
  16.     publiclong getId(){  
  17.         return id;  
  18.     }  
  19.     public String getUserName(){  
  20.         returnthis.userName;  
  21.     }  
  22.     publicvoid setUserName(String n){  
  23.         this.userName = n;  
  24.     }  
  25.     public String getPassWord(){  
  26.         returnthis.passWord;  
  27.     }  
  28.     publicvoid setPassWord(String p){  
  29.         this.passWord = p;  
  30.     }  
  31.     public String getFullName(){  
  32.         returnthis.fullName;  
  33.     }  
  34.     publicvoid setFullName(String f){  
  35.         this
    .fullName = f;  
  36.     }  
  37. }  
資料來源(在spring配置檔案中配置):
  1. <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
  2.     <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
  3.     <propertyname="url"value="jdbc:mysql://localhost:3306/spitter"/>
  4.     <propertyname="username"value="root"/>
  5.     <propertyname="password"value="root"/>
  6.     <propertyname="initialSize"value="5"/>
  7.     <propertyname="maxActive"value="10"/>
  8. </bean>

SessionFactory類(在spring配置檔案中配置):
  1. <beanid="sessionFactory"
  2.     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  3.     <propertyname="dataSource"ref="dataSource"/>
  4.     <propertyname="annotatedClasses">
  5.         <list>
  6.             <value>Spitter.spitterOne.Spitter</value>
  7.         </list>
  8.     </property>
  9.     <propertyname="hibernateProperties">
  10.         <props>
  11.             <propkey="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
  12.             <propkey="hibernate.show_sql">true</prop>
  13.             <propkey="hibernate.format_sql">true</prop>
  14.             <propkey="hibernate.hbm2ddl.auto">update</prop>
  15.         </props>
  16.     </property>
  17. </bean>
配置hibernate事務:
  1. <!-- 設定transactionManager -->
  2.     <beanid="txManager"
  3.        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  4.        <propertyname="sessionFactory"ref="sessionFactory"/>
  5.     </bean>
  6.    <!--啟動spring事務註解功能-->
  7.    <tx:annotation-driventransaction-manager="txManager"/>


進行持久化操作的DAO類:

  1. @Repository
  2. publicclass HibernateSpitterDao implements SpitterDAO {  
  3.     private SessionFactory sessionFactory;  
  4.     @Autowired
  5.     public HibernateSpitterDao(SessionFactory sessionFactory){  
  6.         this.sessionFactory = sessionFactory;  
  7.     }  
  8.     private Session currentSession(){  
  9.         returnthis.sessionFactory.getCurrentSession();  
  10.     }  
  11.     /** 
  12.      * 進行持久化的方法需要使用@Transactional進行事務管理 
  13.      */
  14.     @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
  15.     publicvoid addSpitter(Spitter spitter){  
  16.         this.currentSession().save(spitter);  
  17.     }  
  18.     public Spitter getSpitterById(long id){  
  19.         return (Spitter)this.currentSession().get(Spitter.class, id);  
  20.     }  
  21.     @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
  22.     publicvoid saveSpitter(Spitter spitter){  
  23.         this.currentSession().update(spitter);  
  24.     }  
  25.     publicstaticvoid main(String [] args){  
  26.         Spitter ss = new Spitter(103,"zhang sfdasf454352an""cccninini""zhang shan fdasfsdfewe");  
  27.         ApplicationContext ctx = new ClassPathXmlApplicationContext("Spitter/spitterOne/spring-idol.xml");  
  28.                 <span style="color:#ff0000;">SpitterDAO dao = (SpitterDAO) ctx.getBean("hibernateSpitterDao");</span>  
  29.         dao.addSpitter(ss);  
  30.     }  
  31. }  


因為Spring只能對介面進行aop操作,所以紅色程式碼部分只能將hibernateSpitterDao強制轉換成SpitterDAO介面。