本文是在上一篇博文的基礎之上,通過繼承HibernateDaoSupport調用hibernateTemplate類,只要將UserDaoImpl.java 改為繼承HibernateDaoSupport類,還有spring-dao.xml文件加入對HibernateDaoSupport的配置bean即可,具體實現源碼如 下:
UserDaoImpl.java
package www.csdn.spring.hibernate.dao.copy; import java.util.List; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import www.csdn.spring.hibernate.domain.Users; public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{ @Override public void save(Users entity) { getHibernateTemplate().save(entity); } @Override public List<Users> getObjects(Class clazz) { return getHibernateTemplate().find("from "+clazz.getName()); } @Override public void deleteById(Class clazz,Integer id) { //getHibernateTemplate().delete(hibernateTemplate.get(clazz.getName(), id)); getHibernateTemplate().bulkUpdate("delete from "+clazz.getName()+" where id="+id); } @Override public void update(Users entity) { getHibernateTemplate().update(entity); } }
spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true"> <property name="hibernateTemplate" ref="hibernateTemplate"> </property> </bean> <bean id="usersDaoImpl" class="www.csdn.spring.hibernate.dao.copy.UsersDaoImpl" parent="hibernateDaoSupport"> </bean> </beans>