搭建事务管理转账案例的环境(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
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类)的更多相关文章
- spring框架学习笔记7:事务管理及案例
Spring提供了一套管理项目中的事务的机制 以前写过一篇简单的介绍事务的随笔:http://www.cnblogs.com/xuyiqing/p/8430214.html 还有一篇Hibernate ...
- Spring 框架的事务管理
1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...
- SSM(spring mvc+spring+mybatis)学习路径——1-2、spring事务管理
目录 1-2 Spring事务管理 概念介绍 事务回顾 事务的API介绍 Spring 事务管理 转账案例 编程式事务管理 声明式事务管理 使用XML配置声明式事务 基于tx/aop 使用注解配置声明 ...
- 二十 Spring的事务管理及其API&事务的传播行为,编程式&声明式(xml式&注解式,底层AOP),转账案例
Spring提供两种事务方式:编程式和声明式(重点) 前者需要手写代码,后者通过配置实现. 事务的回顾: 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败 事务的特性:ACI ...
- Spring 事务管理案例
事务管理简介 Spring 事务管理有两种方式:一种是编程式事务管理,即通过编写代码实现事物管理,包括定义事务的开始,程序正常执行后的事物提交,异常时进行的事务回滚.另一种是基于AOP技术实现的声 ...
- Spring 声明式事务管理方式
声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例) 接口Service public i ...
- Spring.NET的中间数据层(Middle Tier Data Access)——事务管理(Transaction management)
简介 Spring.NET为事务管理提供了一个持久化抽象(consistent abstraction ),其优点如下: 为不同事务API,例如ADO.NET,Enterprise Services, ...
- Spring事务管理之编程式事务管理
© 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...
- 深入学习Spring框架(四)- 事务管理
1.什么是事务? 事务(Transaction)是一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位.事务是为了保证数据库的完整性.例如:A给B转账,需 ...
随机推荐
- reduce|sum
reduce() 函数会对参数序列中元素进行累积. 函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1.2 个 ...
- linux优化之全过程
基于开放源代码的Linux给用户提供了这样一个平台:可以根据自己的软.硬件环境,定制自己的Linux应用环境.因此,根据每个用户不同的应用范围定制应用环境,可以将Linux系统的性能提升到新的高度. ...
- Mac上如何用命令修改文件内容
首先打开iTerm,切到文件所在的文件夹目录下 cd xx 然后进入编辑模式 vim xx.xx 然后插入修改 shift + i 修改之后退出插入模式 esc 保存退出 shift + : wq
- 自定义服务与调用--------factory
自定义服务: angular.module('myApp').factory('UserService',['$http','$q',function ($http,$q) { // 定义一个方法工厂 ...
- Java 权限框架 Shiro 实战二:与spring集成、filter机制
转自:https://www.cnblogs.com/digdeep/archive/2015/07/04/4620471.html Shiro和Spring的集成,涉及到很多相关的配置,涉及到shi ...
- Spring MVC 异常处理 - DefaultHandlerExceptionResolver
对一些特殊的异常进行处理,比如方法类型不匹配, 转换错误.
- python 1 面向对象基础知识
1.编码范式 编程 是程序员用特定的 语法+数据结构+算法 组成的代码来告诉计算机如何执行任务的过程 如果把编程比作习武,编程方式就是武林中的各种流派,而在编程的世界里面最常见的两大流派是:面向过程 ...
- autolayout UILabel 设置最大宽度
label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数
- salt常用模块及API
saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能,常用工具支持等,更多模块信息见官网模块介绍:https://docs.saltstack.com/en/latest/ref/mod ...
- conflicting types for ‘方法名’ 的错误
将main()的实现写在drawShapes(),drawCircle(),drawRectangle()...之前. 结果编译的时候出现了 conflicting types for " ...