一共有两种事务管理机制:编程式事务管理和声明式事务管理。

1.编程式事务管理企业已经不用了。

2.我们主要讲的是声明式事务管理。声明式事务管理 ,无需要修改原来代码,只需要配置,为目标代码添加事务管理 , AOP底层实现 --- 企业推荐

下面写一个案例:通过Spring声明式事务管理。实现转账案例。

第一步:建表;

建表语句如下:account中有两个账户(aaa和bbb)

CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');

创建工程 spring3_day3_transactionmanager

导入IoC核心 4个、 日志2个

测试 1个

AOP 4个

Jdbc 2个

数据库驱动1 个

C3p0 1个

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAFbCAIAAAArzwLgAAAgAElEQVR4nO2dTYwcx3XHV/ccDDhAkEOACEjkIDoI0MFA5mTl4kDHnGzDXMM5DBAgsAMFiLUJZFuwpRCChYzFCJYRg6YDGVESraJE4Xi9JkXGkm1xZcsyYtEyBgppfTg0KeuLH7sklzs5zM5MfbxX3TNT3dNd9fthD+R0dfWr1z3/qeqeef+VYWM4ceLEwYMHta3P3PfBnXOP7Jx75J2Xe+dePHj25N0vP/mXz9z3QfNv4RD63ZVu33pl0Ou4LwEAiKwsO4B9jh8/fuutt95yyy1ag2P33b7zznOBv2P33R46wPnHHrvnz394PhzFWE+nKjr6V7+7sg/aCgAajdDTkZh+5CMfCenp527fOfdk4O/wJ3636Div/fAfPvzwPR9++J4HXpEbyHo6VdF+F0UFAI3l6+lETD/60Y8G9PS/Pnf7ztkjk7/1u24b/fktxRctzj/22D0f/vZL/gZtfjppwPIfAFSWrKemmIb1dP2uW3de7o3+1u+6befsI6O/4XD4/i+8tHLTgys3Pfj+L7w0DOnpnPNT9BQAyrBkPT148OAtBv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6O/IkXuXHQ4A5Eu79fTQ1tbq+tahra3h7ntPbG0d+PL6HUdOLjsoAMiUNunpCxuf3vzKLZtfueU7X/3Dt8/9+MSJwwce/8He9XfOnDn8+qnHD21trW5tHfjyeoxZahW/pPFqiQJAWrRJTze/csvejbf3rr564+KLe9ffXl0/tfX6c8Pddx966K9PPfflM2dOfnX9s6uHtlbXnlj4UOgpAMxMm/T0hY1P/8cDNz/5wM1Pfen3f/3GCwce/8HXnz7+2nP/tr7+mfVTn7n3yB1PPLG2uraKngLAUmiTng6Hw7WHN/73/y6+e/nq3t7e6vrWoVOnPv74qY+vrx86tPrJe+9YW1u949570VMAWApt0tMvffPZVy9cefPd7YtXdm7s7Z08eeRDnz28ur514KFThz7wB2trq2tra6trT/j3T6MbMZlV9MY99rsrnV7f8BEZ720cGj0FSJnW6Ok3nnr+sWOnX3vzylsXd7Z3rg739m7s7a2ub33os4cPPH7qwN3rB+5eX117Ym1tzd+3CiOm/d0cUZ5Wi/aLFKOnAInTGj394te+8/QLr7164cqFd3cuXdnZvnp198be9d29UjtHM2IycaylNJejyb/RU4DEaY2efuqBJ384+PUr56688evtC+9uv3Vx58K7O1ev3xiWsoGqwogJPQUAi9bo6Sc//y8/Ofvuz16/+Mq5y69e2H7tze1fXNh+5dzlYRkbqFhGTEZF4pFMei+Mu0FPAfKjNXr6qQee/P7PfvU/v3jv9GuXXn7j0s9/eennb1x66bWLkwZhG6g4RkzjHY2S+pN+0VOA3Pl/vkHWMbw/YbwAAAAASUVORK5CYII=" alt="" />

引入applicationContext.xml 、 db.properties、 log4j.properties

第二步:写实体类:

25Spring_事务管理的两种机制-LMLPHP
//对应数据库中的account表
public class Account {
private int id;
private String name;
private double money;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the money
*/
public double getMoney() {
return money;
}
/**
* @param money the money to set
*/
public void setMoney(double money) {
this.money = money;
} }
25Spring_事务管理的两种机制-LMLPHP

第三步:写 AccountDao接口:

25Spring_事务管理的两种机制-LMLPHP
public interface AccountDao {

//加钱
public void addmoney(String name,double money);
//减钱
public void reducemoney(String name ,double money);
}
25Spring_事务管理的两种机制-LMLPHP

第四步:写AccountDao接口的实现类

25Spring_事务管理的两种机制-LMLPHP
//继承了JdbcDaoSupport,这样就可以Spring的JDBc的模板
public class AccountDaoimpl extends JdbcDaoSupport implements AccountDao {
public void addmoney(String name, double money) {
String sql="update account set money=money+"+money+"where name=?";
this.getJdbcTemplate().update(sql, name);
} public void reducemoney(String name, double money) {
String sql="update account set money=money-"+money+"where name=?";
this.getJdbcTemplate().update(sql, name); } }
25Spring_事务管理的两种机制-LMLPHP

第五步:写转账的接口

25Spring_事务管理的两种机制-LMLPHP
/*
* 转账的接口
*/
public interface AccountService {
//转钱
public void transfer(String outAccount,String inAccount,double money); }
25Spring_事务管理的两种机制-LMLPHP

第六步:转账接口的实现类

里面的accountDao用@AutoWired注解的方式。

25Spring_事务管理的两种机制-LMLPHP
public class AccountServiceimpl implements AccountService {

  /**
* 这个类是转账接口的实现类。我们要考虑的问题是怎么把Dao给注入进去。
* 这里用注解的方式 @Autowired。
* 一旦用这种方式,之前那种set/get方式在applicationContext.xml中的
* <bean id="AccountService" class="cn.itcast.service.AccountServiceimpl">
<!-- <property name="accountDao" ref="AccountDao"/> -->
</bean>
里面的property name="accountDao" ref="AccountDao"/>必须去掉。
*
*
*/
@Autowired
private AccountDao accountDao; public void transfer(String outAccount, String inAccount, double money) { accountDao.addmoney(inAccount, money);
accountDao.reducemoney(outAccount, money); } }
25Spring_事务管理的两种机制-LMLPHP

第七步:写applicationContext.xml

25Spring_事务管理的两种机制-LMLPHP
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> -->
<!-- 引入peoperties文件 -->
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db.properties"/>
</bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean> <bean id="AccountDao" class="cn.itcast.dao.AccountDaoimpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="AccountService" class="cn.itcast.service.AccountServiceimpl"> </bean>
</beans>
25Spring_事务管理的两种机制-LMLPHP

第八步:写Junit测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestAccount {
@Autowired
private AccountService accountService; @Test
public void accounttest()
{ accountService.transfer("aaa", "bbb", 200);
}
}

结果:转账一切正常。但是我们自己在转账函数里面加一个异常:

 *转账接口的实现类
*/
public class AccountServiceimpl implements AccountService { /**
* 这个类是转账接口的实现类。我们要考虑的问题是怎么把Dao给注入进去。
* 这里用注解的方式 @Autowired。
* 一旦用这种方式,之前那种set/get方式在applicationContext.xml中的
* <bean id="AccountService" class="cn.itcast.service.AccountServiceimpl">
<!-- <property name="accountDao" ref="AccountDao"/> -->
</bean>
里面的property name="accountDao" ref="AccountDao"/>必须去掉。
*
*
*/
@Autowired
private AccountDao accountDao; public void transfer(String outAccount, String inAccount, double money) { accountDao.addmoney(inAccount, money);
int a=1/0;
accountDao.reducemoney(outAccount, money); } }

那么会就加钱成功。减钱不成功,所以这里就要加入事务。

05-08 08:43