1. 1. 步骤一:创建WEB工程,引入需要的jar
  2. * IOC6个包
  3. * AOP4个包
  4. * C3P01个包
  5. * MySQL的驱动包
  6. * JDBC目标2个包
  7. * 整合JUnit测试包
    2.步骤二:创建数据库的表结构
  1. create database spring_day03;
  2. use spring_day03;
  3. create table t_account(
  4. id int primary key auto_increment,
  5. name varchar(20),
  6. money double
  7. );
  1. 3.步骤三:引入配置文件
  1. * 引入配置文件
  2. * 引入log4j.properties
  1. 4. 步骤四:引入applicationContext.xml
  1.   * 基本配置为:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
  7.  
  8. </beans>

  *  使用C3P0连接池

  1. * 先引入C3P0jar
  2. * com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
  3. * 编写配置文件
  4. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  5. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  6. <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
  7. <property name="user" value="root"/>
  8. <property name="password" value="root"/>
  9. </bean>
  1. 5.步骤五:创建对应的包结构和类
  1. * com.huida.demo1
  2. * AccountService
  1. package com.huida.demo1;
  2.  
  3. public interface AccountService {
  4.  
  5. public void pay(String out,String in,double money);
  6. }
  1. * AccountServlceImpl
  1. package com.huida.demo1;
  2.  
  3. import javax.annotation.Resource;
  4.  
  5. import org.springframework.transaction.TransactionStatus;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  8. import org.springframework.transaction.support.TransactionTemplate;
  9.  
  10. public class AccountServiceImpl implements AccountService{
  11.  
  12. @Resource(name="accountDao")
  13. private AccountDaoImpl accountDao;
  14.  
  15. public void setAccountDao(AccountDaoImpl accountDao) {
  16. this.accountDao = accountDao;
  17. }
  18. @Override
  19. public void pay(String out,String in,double money) {
  20.  
  21. //扣钱
  22. accountDao.outMoney(out, money);
  23. //加钱
  24. accountDao.inMoney(in, money);
  25. }
  26.  
  27. }
  1. * AccountDao
  1. package com.huida.demo1;
  2.  
  3. public interface AccountDao {
  4.  
  5. public void outMoney(String out,double money);
  6. public void inMoney(String in,double money);
  7. }
  1. * AccountDaoImpl
  1. package com.huida.demo1;
  2.  
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4.  
  5. public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
  6.  
  7. @Override
  8. public void outMoney(String out, double money) {
  9.  
  10. this.getJdbcTemplate().update("update user set money = money - ? where name = ?", money,out);
  11.  
  12. }
  13.  
  14. @Override
  15. public void inMoney(String in, double money) {
  16.  
  17. this.getJdbcTemplate().update("update user set money = money + ? where name=?",money,in);
  18.  
  19. }
  20. }
  1. 6.步骤六:引入Spring的配置文件,将类配置到Spring
  1. <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
  2. </bean>
  3. <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
  4. </bean>
  1. 7.步骤七:在业务层注入DAO ,在DAO中注入JDBC模板(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
  1. <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
  2. <property name="accountDao" ref="accountDao"/>
  3. </bean>
  4. <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
  5. <property name="dataSource" ref="dataSource"/>
  6. </bean>
  1. 8.所以总的applicationContext2.xml中的配置信息为:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
  7.  
  8. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  9. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  10. <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
  11. <property name="user" value="root"/>
  12. <property name="password" value="root"/>
  13. </bean>
  14.  
  15. <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
  16. <property name="dataSource" ref="dataSource"/>
  17. </bean>
  18. <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
  19. <property name="accountDao" ref="accountDao"/>
  20. </bean>
  21.  
  22. </beans>
  1. 9.步骤八:编写测试程序.
  1. package com.huida.demo1;
  2.  
  3. import javax.annotation.Resource;
  4.  
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9.  
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration("classpath:applicationContext2.xml")
  12. public class Demo1 {
  13.  
  14. @Resource(name="accountService")
  15. private AccountService accountService;
  16.  
  17. @Test
  18. public void run1(){
  19. accountService.pay("小明","小红",1000);
  20. }
  21. }
  1. 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. reduce|sum

    reduce() 函数会对参数序列中元素进行累积. 函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1.2 个 ...

  2. linux优化之全过程

    基于开放源代码的Linux给用户提供了这样一个平台:可以根据自己的软.硬件环境,定制自己的Linux应用环境.因此,根据每个用户不同的应用范围定制应用环境,可以将Linux系统的性能提升到新的高度. ...

  3. Mac上如何用命令修改文件内容

    首先打开iTerm,切到文件所在的文件夹目录下 cd xx 然后进入编辑模式 vim xx.xx 然后插入修改 shift + i 修改之后退出插入模式 esc 保存退出 shift + :  wq

  4. 自定义服务与调用--------factory

    自定义服务: angular.module('myApp').factory('UserService',['$http','$q',function ($http,$q) { // 定义一个方法工厂 ...

  5. Java 权限框架 Shiro 实战二:与spring集成、filter机制

    转自:https://www.cnblogs.com/digdeep/archive/2015/07/04/4620471.html Shiro和Spring的集成,涉及到很多相关的配置,涉及到shi ...

  6. Spring MVC 异常处理 - DefaultHandlerExceptionResolver

    对一些特殊的异常进行处理,比如方法类型不匹配, 转换错误.

  7. python 1 面向对象基础知识

    1.编码范式 编程  是程序员用特定的 语法+数据结构+算法 组成的代码来告诉计算机如何执行任务的过程 如果把编程比作习武,编程方式就是武林中的各种流派,而在编程的世界里面最常见的两大流派是:面向过程 ...

  8. autolayout UILabel 设置最大宽度

    label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数

  9. salt常用模块及API

    saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能,常用工具支持等,更多模块信息见官网模块介绍:https://docs.saltstack.com/en/latest/ref/mod ...

  10. conflicting types for ‘方法名’ 的错误

    将main()的实现写在drawShapes(),drawCircle(),drawRectangle()...之前. 结果编译的时候出现了  conflicting types for " ...