本文是在上一篇博文的基础之上,通过继承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>
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索hibernate
, spring
, 增删改查
, hibernatetemplate
, spring整合hibernate
, override
, public
, gethibernatetemplate
, hibernatedaosupport
, 注解配置增删改查
, xml增删改查
Spring与Hibernate
spring hibernate整合、spring整合hibernate5、spring整合hibernate4、spring整合hibernate3、hibernate增删改查,以便于您获取更多的相关知识。