標籤:儲存 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持久層模板於交易管理