关于spring 与Hibernate 结合使用的事务管理
2017-12-22 11:07
525 查看
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:p="http://www.springframework.org/schema/p"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3305/structs_db</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/lucas/bean/User.hbm.xml</value>
<value>com/lucas/bean/Scary.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="scaryDaoImpl" class="com.lucas.dao.impl.ScaryDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method name="tranfer" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.lucas.dao.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<bean id="loginAction" class="com.lucas.action.LoginAction" scope="prototype">
<property name="userDaoImpl" ref="userDaoImpl"/>
</bean>
</beans>
实现类中是这样的:
package com.lucas.dao.impl;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.lucas.dao.ScaryDao;
public class ScaryDaoImpl extends HibernateTemplate implements ScaryDao {
@Override
public void tranfer(){
// TODO Auto-generated method stub
String sql1="update Scary as s set s.banlace=s.banlace+1000 where s.id=1";
String sql2="update Scary as s set s.banlace=s.banlace-1000 wdhere s.id=2";
super.getSession().createQuery(sql1).executeUpdate();
super.getSession().createQuery(sql2).executeUpdate();
}
}
以及
package com.lucas.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lucas.bean.Scary;
import com.lucas.dao.ScaryDao;
public class ScaryDaoImplTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testTranfer() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ScaryDao ud=(ScaryDao)ac.getBean("scaryDaoImpl");
ud.tranfer();
}
}
可实现事务
<?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:p="http://www.springframework.org/schema/p"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3305/structs_db</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/lucas/bean/User.hbm.xml</value>
<value>com/lucas/bean/Scary.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="scaryDaoImpl" class="com.lucas.dao.impl.ScaryDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method name="tranfer" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.lucas.dao.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
<bean id="loginAction" class="com.lucas.action.LoginAction" scope="prototype">
<property name="userDaoImpl" ref="userDaoImpl"/>
</bean>
</beans>
实现类中是这样的:
package com.lucas.dao.impl;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.lucas.dao.ScaryDao;
public class ScaryDaoImpl extends HibernateTemplate implements ScaryDao {
@Override
public void tranfer(){
// TODO Auto-generated method stub
String sql1="update Scary as s set s.banlace=s.banlace+1000 where s.id=1";
String sql2="update Scary as s set s.banlace=s.banlace-1000 wdhere s.id=2";
super.getSession().createQuery(sql1).executeUpdate();
super.getSession().createQuery(sql2).executeUpdate();
}
}
以及
package com.lucas.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lucas.bean.Scary;
import com.lucas.dao.ScaryDao;
public class ScaryDaoImplTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testTranfer() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ScaryDao ud=(ScaryDao)ac.getBean("scaryDaoImpl");
ud.tranfer();
}
}
可实现事务
相关文章推荐
- 使用spring管理hibernate的事务
- hibernate使用spring的事务管理器在测试dao的时候获取不到session
- Spring整合hibernate:3、使用XML进行声明式的事务管理
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- Spring2.5整合Hibernate3.0中使用XML以tx来配置事务管理。
- 关于spring hibernate的事务管理
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- Java学习之道:纯Hibernate使用-不用spring事务管理
- Spring 使用Hibernate事务管理:update等操作无效果问题
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- Spring和Hibernate的结合--声明式事务管理(转)
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- 一些关于hibernate 与 spring 事务管理的问题
- Spring整合hibernate:3、使用XML进行声明式的事务管理
- Spring整合hibernate:3、使用XML进行声明式的事务管理
- hibernate,spring管理事务中(transaction,JDBC connection,Hibernate Session的使用)
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- Spring和Hibernate使用注解方式进行事务管理
- 关于hibernate与spring的结合使用中的配置文件