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



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


持久化類:

@Entity  
public class Spitter {  
    private long id;  
    private String userName, passWord, fullName;  
      
    public Spitter(long id, String n, String p, String f){  
        this.id = id;  
        this.userName = n;  
        this.passWord = p;  
        this.fullName = f;  
    }  
    public Spitter(){}  
    public void setId(long id){  
        this.id = id;  
    }  
    @Id  
    public long getId(){  
        return id;  
    }  
    public String getUserName(){  
        return this.userName;  
    }  
    public void setUserName(String n){  
        this.userName = n;  
    }  
    public String getPassWord(){  
        return this.passWord;  
    }  
    public void setPassWord(String p){  
        this.passWord = p;  
    }  
    public String getFullName(){  
        return this.fullName;  
    }  
    public void setFullName(String f){  
        this.fullName = f;  
    }  
  
}



數據源(在spring配置文件中配置):

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >  
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    <property name="url" value="jdbc:mysql://localhost:3306/spitter" />  
    <property name="username" value="root" />  
    <property name="password" value="root" />  
    <property name="initialSize" value="5"/>  
    <property name="maxActive" value="10" />  
</bean>


SessionFactory類(在spring配置文件中配置):

<bean id="sessionFactory"   
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
    <property name="dataSource" ref="dataSource" />  
    <property name="annotatedClasses">  
        <list>  
            <value>Spitter.spitterOne.Spitter</value>  
        </list>  
    </property>  
  
    <property name="hibernateProperties">  
        <props>  
            <prop key="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>  
            <prop key="hibernate.show_sql">true</prop>  
            <prop key="hibernate.format_sql">true</prop>  
            <prop key="hibernate.hbm2ddl.auto">update</prop>  
              
        </props>  
    </property>  
      
</bean>




配置hibernate事務:


<!-- 設定transactionManager -->    
    <bean id="txManager"    
       class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
       <property name="sessionFactory" ref="sessionFactory" />    
    </bean>    
   
   <!--啓動spring事務註解功能-->    
   <tx:annotation-driven transaction-manager="txManager"/>



進行持久化操作的DAO類:


@Repository  
public class HibernateSpitterDao implements SpitterDAO {  
    private SessionFactory sessionFactory;  
      
    @Autowired  
    public HibernateSpitterDao(SessionFactory sessionFactory){  
        this.sessionFactory = sessionFactory;  
    }  
    private Session currentSession(){  
        return this.sessionFactory.getCurrentSession();  
    }  
    /** 
     * 進行持久化的方法需要使用@Transactional進行事務管理 
     */  
    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
    public void addSpitter(Spitter spitter){  
        this.currentSession().save(spitter);  
    }  
    public Spitter getSpitterById(long id){  
        return (Spitter)this.currentSession().get(Spitter.class, id);  
    }  
    @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
    public void saveSpitter(Spitter spitter){  
        this.currentSession().update(spitter);  
    }  
    public static void main(String [] args){  
        Spitter ss = new Spitter(103,"zhang sfdasf454352an", "cccninini", "zhang shan fdasfsdfewe");  
      
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Spitter/spitterOne/spring-idol.xml");  
                <span style="color:#ff0000;">SpitterDAO dao = (SpitterDAO) ctx.getBean("hibernateSpitterDao");</span>  
        dao.addSpitter(ss);  
          
    }  
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章