spring与hibernate整合

spring-hibernate.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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">

	<!-- 
		LocalSessionFactoryBean 该类负责根据配置信息创建SessionFactory 对象
	 -->
	<bean id="WangWu" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 
			将原hibernate.cfg.xml中的原始配置,由Spring的配置替代
		 -->
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.connection.username">scott</prop>
				<prop key="hibernate.connection.password">tiger</prop>
				<prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</prop>
				<prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>cn/veryedu/entity/AccountsTab.hbm.xml</value>
			</list>
		</property>
		
		  <!-- 
		 <property name="configLocation">
		 	<value>classpath:hibernate.cfg.xml</value>
		 </property>
		  -->
	</bean>
	
	<!-- 
		使用Spring为Hibernate定义的HibernateTemplate模板
	 -->
	 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref local="WangWu"/>
		</property>
	 </bean>

	<!-- 
		配置Spring中提供hibernate事务管理对象
	 -->
	 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	 	<property name="sessionFactory">
	 		<ref local="WangWu"/>
	 	</property>
	 </bean>
	
	<!-- 
		给出AOP配置,让事务应用到各级代码中
	 -->
	 <aop:config>
	 	<aop:pointcut id="transactionPointcut" expression="execution(* cn.veryedu.service..*.*(..))"/>
	 	<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
	 </aop:config>
	 
	 <!-- 
	 	Spring中专门用于定义事务管理的通知定义元素
	  -->
	 <tx:advice id="transactionAdvice">
	 	<tx:attributes>
	 		<tx:method name="add*" propagation="REQUIRED"/>
	 		<tx:method name="del*" propagation="REQUIRED"/>
	 		<tx:method name="update*" propagation="REQUIRED"/>
	 		<tx:method name="modify*" propagation="REQUIRED"/>
	 		<tx:method name="find*" read-only="true"/>
	 	</tx:attributes>
	 </tx:advice>
	
	
	
	<!-- 
		自定义DAO
	 -->
	<bean id="accountsTabDAO" class="cn.veryedu.dao.impl.AccountsTabDAOImpl">
		<property name="hibernateTemplate">
			<ref local="hibernateTemplate"/>
		</property>
	</bean>	

</beans>	


dao实现类中的配置

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.veryedu.dao.DAO;
public class AccountsTabDAOImpl extends HibernateDaoSupport implements DAO {

public List findByAll() {   /*    this.getHibernateTemplate().execute(      new HibernateCallback() {

     @Override      public Object doInHibernate(Session session)        throws HibernateException, SQLException {       // TODO Auto-generated method stub              session.createQuery(arg0)       return null;      }            }    );    */   List result = this.getHibernateTemplate().find("from AccountsTab");   return result;  }

 public AccountsTab findByPrimaryKey(Long primaryKey) {      AccountsTab accountsTab       = (AccountsTab)         this.getHibernateTemplate().get(AccountsTab.class,              primaryKey);   this.getHibernateTemplate().evict(accountsTab);   return accountsTab;  }

 public void update(AccountsTab account) {   this.getHibernateTemplate().update(account);  }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值