Persistent layer as long as the completion of data on the database additions and deletions to the operation, we often say that hibernate is different from MyBatis is in his automatic, and Hibernate's automatic is mainly reflected in
His template, some simple data manipulation we will not have to write the SQL, can be done by the configuration of Hibernate template.
First, inject hibernate template in DAO
1.1 Inherit the hibernate template provided by spring in the DAO layer
Modify the Entity product class so that he inherits Hibernatedaosupport
1.2 Configuring the DAO Layer injection hibernate template in Applicationcontext.xml
<!--Configure DAO Layer: Inject Hibernate template-- <bean id= "Productdao" class= "Com.ssh.dao.productDao" > <!--ref The value is consistent with the Sessionfactory Bean ID-- <property name= "sessionfactory" ref= "Sessionfactory" ></property > </bean>
1.3 The Hibernate template is called at the DAO layer to complete the data operation: (Template calls This.gethibernatetemplate ().)
public void Sava (product product) {SYSTEM.OUT.PRINTLN ("Business layer invoke DAO layer succeeded!") ");//Call Hibernate template to complete save Data Operation This.gethibernatetemplate (). Save (product);
Second, add transaction management
2.1 Configuring Transaction Management
<!--configuration transaction Manager- <bean id= "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "></property> </bean>
2.2 Open Transaction Management
<!--open annotation transaction-- <tx:annotation-driven transaction-manager= "TransactionManager"/>
2.3 Introducing transaction management mechanisms at the business level @transactional
SSH (vi) Hibernate persistence layer template in transaction management