Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

spring入门(11)-spring与hibernate整合完成增删改查的操作(继承HibernateDaoSupport调用hibernateTemplate类)

$
0
0

本文是在上一篇博文的基础之上,通过继承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>



 

作者:sgx425021234 发表于2013-5-9 23:34:06 原文链接
阅读:18 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>