Template
(1) Spring when integrating Hibernate for transaction management, there will be some fixed code, such as Userdaoimpl 's Save ( ) method has the following code:
Session s = null;
try {
s = sessionfactory.opensession ();
S.gettransaction (). Begin ();
S.save (user);
S.gettransaction (). commit ();
} catch (Hibernateexception e) {
E.printstacktrace ();
s.gettransaction (). rollback ();
} finally {
if (s! = null) {
S.close ();
s = null;
}
}
(2) When there are multiple transactions, the code will be duplicated, and you can use Spring 's template to write its pinned portions, and then fill in your own logic .
(3) In the configuration file configuration hibernatetemplate class, the class encapsulates the sessionfactory, which has written the fixed code, which calls the related methods of Sessionfactory class
< Bean id= "hibernatetemplate" class=" Org.springframework.orm.hibernate3.HibernateTemplate ">
< Property name="Sessionfactory" ref="Sessionfactory"></Property >
</ Bean >
(4) Modified Userdaoimpl
Package Com.zgy.impl;
Import Javax.annotation.Resource;
Import org.hibernate.HibernateException;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import Org.springframework.orm.hibernate3.HibernateTemplate;
Import org.springframework.stereotype.Component;
Import Com.zgy.dao.UserDAO;
Import Com.zgy.model.User;
@Component ("U")
public class Userdaoimpl implements Userdao {
Private Hibernatetemplate hiberantetemplate;
Public Hibernatetemplate gethiberantetemplate () {
return hiberantetemplate;
}
@Resource
public void Sethiberantetemplate (Hibernatetemplate hiberantetemplate) {
This.hiberantetemplate = hiberantetemplate;
}
public void Save (user user) {
/*
* in general, the following code is required to access the database using sessionfactory , but using the Template can simplify
* Session s = null;
try {
s = sessionfactory.opensession ();
S.gettransaction (). Begin ();
S.save (user);
S.gettransaction (). commit ();
} catch (Hibernateexception e) {
E.printstacktrace ();
S.gettransaction (). rollback ();
} finally {
if (s! = null) {
S.close ();
s = null;
}
}
*/
Hiberantetemplate.save (user);
}
}
(5) Since hiberantetemplate has been injected by spring , call the Save () of the Hibernatetemplate class directly method can be used.
(6) Hibernatetemplate Hibernate exception encapsulated as DataAccessException, which is the Run The subclass of Exception
Hibernatedaosuppport
(1) can abstract a Superdao, package hibernatetemplate
Package Com.zgy.dao;
Import Javax.annotation.Resource;
Import Org.springframework.orm.hibernate3.HibernateTemplate;
Import org.springframework.stereotype.Component;
@Component
public class Superdao {
Private Hibernatetemplate hibernatetemplate;
Public Hibernatetemplate gethibernatetemplate () {
return hibernatetemplate;
}
@Resource
public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {
This.hibernatetemplate = hibernatetemplate;
}
}
(2) Other Xxxdao only need to inherit Superdao, you can get hibernatetemplate
Package Com.zgy.impl;
Import org.springframework.stereotype.Component;
Import Com.zgy.dao.SuperDAO;
Import Com.zgy.dao.LogDAO;
Import Com.zgy.model.Log;
@Component ("Logdao")
public class Logdaoimpl extends Superdao implements Logdao {
@Override
public void Savelog (log log) {
Super.gethibernatetemplate (). Save (log);
}
}
(3) Another way to use hibernatetemplate is to use the hibernatedaosupport class
(4) inheriting hibernatedaosupport in Superdao
Package Com.zgy.dao;
Import Javax.annotation.Resource;
Import Org.springframework.orm.hibernate3.HibernateTemplate;
Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport;
Import org.springframework.stereotype.Component;
@Component
public class Superdao extends hibernatedaosupport{
@Resource (name= "Hibernatetemplate")
public void Setsuperhibernatetemplate (Hibernatetemplate hibernatetemplate) {
Super.sethibernatetemplate (hibernatetemplate);
}
}
(5) Other Xxxdao inherit Superdao
Package Com.zgy.impl;
Import org.springframework.stereotype.Component;
Import Com.zgy.dao.SuperDAO;
Import Com.zgy.dao.LogDAO;
Import Com.zgy.model.Log;
@Component ("Logdao")
public class Logdaoimpl extends Superdao implements Logdao {
@Override
public void Savelog (log log) {
Super.gethibernatetemplate (). Save (log);
}
}
Spring--hibernatetemplate, Hibernatedaotemplate