Spring使用HibernateDaoSupport操作数据


         spring提供了一个数据访问层的类:org.springframework.orm.hibernate3.support.HibernateDaoSupport,通常是让

dao继承该类,然后在dao中定义个set方法用来初始化HibernateDaoSupport的HibernateTemplate或者是

SessionFactory.

           由于 HibernateTemplate和SessionFactory都是fanal类型,因此不能被重写。此时可以随便set一个方法,注入

资源,在set方法里传入SessionFactory或者HibernateTemplate,然后再通过super或者this调用HibernateDaoSupport的

对应set方法。


HibernateDao接口:

package com.dao;

public interface HibernateDao {

}

可以在里面封装一些常用方法;


HibernateDaoImpl:

package com.dao;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao{
	@Resource
	public void setOne(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
}

有set方法之处,就可以注入。setOne注入参数sessionFactory,初始化HibernateDaoSupport的SessionFactory.


applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/tx
	    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd		
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<context:component-scan base-package="com.*" /> 
	<context:annotation-config /> 
	<!-- <tx:annotation-driven transaction-manager="txManager"/> -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:c3p0.properties</value>
		</property>
	</bean>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="packagesToScan">
			<list>
				<value>com.entity</value>
				
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>

	</bean>
	
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="save*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
  <aop:config>
  	<aop:pointcut id="fooServiceOperation" expression="execution(public * com.service..*.save(..))"/>
  	<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
  </aop:config>
</beans>


userDaoImpl:

package com.dao;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.entity.Student;
@Repository(value="userDao")
public class UserDaoImpl extends HibernateDaoImpl implements UserDao{	
	
	public void save(Student student){
		this.getHibernateTemplate().save(student);
		
	}
}

HibernateDao是用来被继承类,可以封装一些常用的方法。

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值