SSH(六)hibernate持久層模板於交易管理

來源:互聯網
上載者:User

標籤:儲存   png   property   script   操作   name   mybatis   XML   增刪改查   

  持久層只要完成資料對資料庫增刪改查的操作,我們常說的hibernate區別於mybatis是在於他的全自動,而hibernate的全自動則主要體現於

他的模板,一些簡單的資料操作我們就不用再去手寫sql了,可以通過對hibernate模板的配置去完成。

一、在dao中注入hibernate模板

1.1在dao層繼承spring提供的hibernate模板

修改實體product類,讓他繼承HibernateDaoSupport

1.2在applicationcontext.xml中配置dao層注入hibernate模板

        <!-- 配置dao層:注入hibernate模板 -->        <bean id="productDao" class="com.ssh.dao.productDao">        <!-- ref 值與sessionFactory bean id保持一致 -->        <property name="sessionFactory" ref="sessionFactory"></property>        </bean>

1.3在dao層調用hibernate模板完成資料操作:(模板調用this.getHibernateTemplate().)

public void sava(product product){System.out.println("業務層調用dao層成功!");//調用hibernate模板完成儲存資料操作this.getHibernateTemplate().save(product);}

二、添加交易管理

2.1配置交易管理

        <!-- 配置交易管理員 -->        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>        </bean>

2.2開啟交易管理

        <!-- 開啟註解事務 -->        <tx:annotation-driven transaction-manager="transactionManager"/>

2.3在業務層引入交易管理機制@Transactional

 

SSH(六)hibernate持久層模板於交易管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.