1. 步骤一:创建WEB工程,引入需要的jar包
* IOC的6个包
* AOP的4个包
* C3P0的1个包
* MySQL的驱动包
* JDBC目标2个包
* 整合JUnit测试包

2.步骤二:创建数据库的表结构
    create database spring_day03;
use spring_day03;
create table t_account(
id int primary key auto_increment,
name varchar(20),
money double
);
3.步骤三:引入配置文件
    * 引入配置文件
* 引入log4j.properties
4. 步骤四:引入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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> </beans>

  *  使用C3P0连接池

    * 先引入C3P0的jar包
* com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar * 编写配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
5.步骤五:创建对应的包结构和类
    * com.huida.demo1
* AccountService
package com.huida.demo1;

public interface AccountService {

    public void pay(String out,String in,double money);
}
        * AccountServlceImpl
package com.huida.demo1;

import javax.annotation.Resource;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; public class AccountServiceImpl implements AccountService{ @Resource(name="accountDao")
private AccountDaoImpl accountDao; public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao = accountDao;
}
@Override
public void pay(String out,String in,double money) { //扣钱
accountDao.outMoney(out, money);
//加钱
accountDao.inMoney(in, money);
} }
        * AccountDao
package com.huida.demo1;

public interface AccountDao {

    public void outMoney(String out,double money);
public void inMoney(String in,double money);
}
        * AccountDaoImpl
package com.huida.demo1;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{

    @Override
public void outMoney(String out, double money) { this.getJdbcTemplate().update("update user set money = money - ? where name = ?", money,out); } @Override
public void inMoney(String in, double money) { this.getJdbcTemplate().update("update user set money = money + ? where name=?",money,in); }
}
6.步骤六:引入Spring的配置文件,将类配置到Spring中
    <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
</bean> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
</bean>
7.步骤七:在业务层注入DAO ,在DAO中注入JDBC模板(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
    <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
8.所以总的applicationContext2.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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean> </beans>
9.步骤八:编写测试程序.
package com.huida.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo1 { @Resource(name="accountService")
private AccountService accountService; @Test
public void run1(){
accountService.pay("小明","小红",1000);
}
}
10.单元测试run1方法,刷新spring-day03数据库中的user表,可以看到小明同学的钱减少了1000,小红同学的钱增加了1000。

搭建事务管理转账案例的环境(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)的更多相关文章

  1. spring框架学习笔记7:事务管理及案例

    Spring提供了一套管理项目中的事务的机制 以前写过一篇简单的介绍事务的随笔:http://www.cnblogs.com/xuyiqing/p/8430214.html 还有一篇Hibernate ...

  2. Spring 框架的事务管理

    1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...

  3. SSM(spring mvc+spring+mybatis)学习路径——1-2、spring事务管理

    目录 1-2 Spring事务管理 概念介绍 事务回顾 事务的API介绍 Spring 事务管理 转账案例 编程式事务管理 声明式事务管理 使用XML配置声明式事务 基于tx/aop 使用注解配置声明 ...

  4. 二十 Spring的事务管理及其API&事务的传播行为,编程式&声明式(xml式&注解式,底层AOP),转账案例

    Spring提供两种事务方式:编程式和声明式(重点) 前者需要手写代码,后者通过配置实现. 事务的回顾: 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败 事务的特性:ACI ...

  5. Spring 事务管理案例

    事务管理简介   Spring 事务管理有两种方式:一种是编程式事务管理,即通过编写代码实现事物管理,包括定义事务的开始,程序正常执行后的事物提交,异常时进行的事务回滚.另一种是基于AOP技术实现的声 ...

  6. Spring 声明式事务管理方式

    声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例)      接口Service public i ...

  7. Spring.NET的中间数据层(Middle Tier Data Access)——事务管理(Transaction management)

    简介 Spring.NET为事务管理提供了一个持久化抽象(consistent abstraction ),其优点如下: 为不同事务API,例如ADO.NET,Enterprise Services, ...

  8. Spring事务管理之编程式事务管理

    © 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...

  9. 深入学习Spring框架(四)- 事务管理

    1.什么是事务? 事务(Transaction)是一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位.事务是为了保证数据库的完整性.例如:A给B转账,需 ...

随机推荐

  1. labview如何生成可执行文件

    labview生成可执行文件可以分为两种情况. 第一种,是电脑中有labview软件开发环境的情况 第二种,是电脑中没有安装labview软件开发环境 下面是一个简单的labview代码: 程序解释: ...

  2. Web of Science数据库中文献相关信息下载与保存

    1. Web of Science 数据库(https://apps.webofknowledge.com/): a. 所在网络必须由访问 该网站的权限. b.建议使用web of Science的核 ...

  3. js数组的初始化

    方法一: var myarray = new Array(66,80,90,77,59); 方法二: var myarray = [66,80,90,77,59]; 方法三: var myarray= ...

  4. xrange

    range返回一个列表 xrange反会xrange这个类的实例,每次遍历返回一个值.比range的执行效率要高 在python3中range = xrange range(1,10,2)     - ...

  5. css的样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. linux case ${variable} in

    脚本实现划分考试等级层次;

  7. 今天练手了下mysqlbinlog,标记下

    1 首先查看是否开启了 bin log 登录mysql后  使用命令 show variables like "log_%"; show binary logs; 2  确认开启了 ...

  8. MySQL 获取最后插入的ID LAST_INSERT_ID用法

    LAST_INSERT_ID() 自动返回最后一个INSERT或 UPDATE 查询中 AUTO_INCREMENT列设置的第一个表发生的值. MySQL的LAST_INSERT_ID的注意事项: 第 ...

  9. ImageIO(图像处理)

    1.通过ImageIO的read和writer,对图像文件进行处理. BufferedImage buffImage = ImageIO.read(file); // 将图像输出到Servlet输出流 ...

  10. webserive学习记录4-获取天气的例子

    学习到了如何创建使用webservice服务,下面就实际应用一下,从网络上获取天气数据. 先从网络上找到免费的webservice服务, 如这个网站:http://www.webxml.com.cn/ ...