Spring 事务管理方式 有两种
1. 编写代码时实现事务 定义事务, 程序正常执行 , 异常抛出事务
2.基于AOP技术实现声明是事务, 可以将事务管理设置 为一"方面"
Spring 2.5 实现声明式事务 基于AOP技术 其优点是无需编程的方式管理事务 只需配置相关配置文件就可以
事务管理主要是任务
创建事务 事务的回滚 事务提交
第一步 实现实体类 和映射文件
hibernate实现 文件
第二步 实现Dao层
package com.sun.Dao;
import java.util.List;
import com.sun.entity.Account;
public interface AccountDao {
public List getAccountByAccountNo(String accountNo);
public void transfer(Account a1,Account a2);
}
package com.sun.Dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sun.entity.Account;
public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {
public List getAccountByAccountNo(final String accountNo) {
// TODO Auto-generated method stub
return super.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session)
throws HibernateException, SQLException {
Criteria c = session.createCriteria(Account.class);
c.add(Restrictions.eq("accountNo", accountNo));
return c.list();
}
});
}
// 转账
public void transfer(Account a1, Account a2) {
// TODO Auto-generated method stub
super.getHibernateTemplate().update(a1);
super.getHibernateTemplate().update(a2);
}
第三步
实现BIz 层
package com.sun.Biz;
import java.util.List;
import com.sun.entity.Account;
public interface AccountBiz {
public List getAccountByAccountNo(String accountNo);
public void transfer(Account a1,Account a2);
}
package com.sun.Biz;
import java.util.List;
import com.sun.Dao.AccountDao;
import com.sun.entity.Account;
public class AccountBizImpl implements AccountBiz {
AccountDao accountdao;
public AccountDao getAccountdao() {
return accountdao;
}
public void setAccountdao(AccountDao accountdao) {
this.accountdao = accountdao;
}
public List getAccountByAccountNo(String accountNo) {
// TODO Auto-generated method stub
return accountdao.getAccountByAccountNo(accountNo);
}
public void transfer(Account a1, Account a2) {
// TODO Auto-generated method stub
accountdao.transfer(a1, a2);
}
}
第四步 创建action
package com.sun.action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.Biz.AccountBiz;
import com.sun.entity.Account;
public class AccountMessage extends ActionSupport {
/**
* @return
*/
private String ac1;
private String ac2;
private String amount;
public String getAc1() {
return ac1;
}
public void setAc1(String ac1) {
this.ac1 = ac1;
}
public String getAc2() {
return ac2;
}
public void setAc2(String ac2) {
this.ac2 = ac2;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
AccountBiz accountbiz;
public AccountBiz getAccountbiz() {
return accountbiz;
}
public void setAccountbiz(AccountBiz accountbiz) {
this.accountbiz = accountbiz;
}
public String execute() throws Exception{
// TODO Auto-generated method stub
Account a1=null;
Account a2 =null;
List list1=accountbiz.getAccountByAccountNo(ac1);
if (list1.size()>0) {
a1=(Account) list1.get(0);
a1.setBalance(new Long(a1.getBalance().longValue()-Long.parseLong(amount)));
}
List list2=accountbiz.getAccountByAccountNo(ac2);
if (list2.size()>0) {
a2=(Account) list2.get(0);
a2.setBalance(new Long(a2.getBalance().longValue()-Long.parseLong(amount)));
}
try {
accountbiz.transfer(a1, a2);
} catch (Exception e) {
// TODO: handle exception
return "error";
}
//获取第二的A2的帐号对象 兵更新对象中的帐号的余额的属性
return SUCCESS;
}
}
第五步 Spring 中配置Dao biz 和 AoccountMessage
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<!-- 定义dao类事例 -->
<bean id="accountdao" class="com.sun.Dao.AccountDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="accountbiz" class="com.sun.Biz.AccountBizImpl">
<property name="accountdao" ref="accountdao"></property>
</bean>
<!-- -部署Struts的责帐 号管理的控制器 accountmessage 并给Accountmessage类中的accountbiz属性组织 -->
<bean id="amAction" class="com.sun.action.AccountMessage">
<property name="accountbiz" ref="accountbiz"></property>
</bean>
</beans>
第六步 Struts.xml中配置accountMessage
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="dotransfer" class="com.sun.action.AccountMessage">
<result name="error">error.jsp</result>
<result name="success">success.jsp</result>
</action>
</package>
</struts>
第七部 配置声明事务
<?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" xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 声明事务管理器 -->
<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义事务通知 -->
<tx:advice id="txAdvice" transaction-manager="myHibTransactionManager">
<!-- 指定事务传播规则 -->
<tx:attributes>
<!-- 对get打头的方法应用SUPPORTS事务规则 -->
<tx:method name="get*" propagation="SUPPORTS" />
<!-- 对其他方法应用REQUIRED事务规则 -->
<tx:method name="*" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 定义一个切面 并将事务和切面结合 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/bookshop">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 定义切面,并将事务通知和切面组合(定义哪些方法应用事务规则) -->
<aop:config>
<!-- 对com.ssh2test1.biz包下的所有类的所有方法都应用事务规则 -->
<aop:pointcut id="bizMethods" expression="execution(* com.sun.Biz.*.*(..))" />
<!-- 将事务通知和切面组合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
最后编写 前台
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'transfer.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<s:head/>
</head>
<body>
<s:form action="dotransfer.action">
<table>
<tr>
<s:textfield name="ac1" label="帐号1"></s:textfield>
</tr>
<tr>
<s:textfield name="ac2" label="帐号2"></s:textfield>
</tr>
<tr>
<s:textfield name="amount" label="转账金额"></s:textfield>
</tr>
<tr>
<s:submit value="转账"></s:submit>
</tr>
</table>
</s:form> <br>
</body>
</html>