一、创建spring项目

项目名称:spring101310

二、在项目上添加jar包

1.在项目中创建lib目录

/lib

2.在lib目录下添加spring支持

com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging.jar

junit-4.10.jar

log4j.jar

mysql-connector-java-5.1.18-bin.jar

spring-aop-3.2.0.RELEASE.jar

spring-aspects-3.2.0.RELEASE.jar

spring-beans-3.2.0.RELEASE.jar

spring-context-3.2.0.RELEASE.jar

spring-core-3.2.0.RELEASE.jar

spring-expression-3.2.0.RELEASE.jar

spring-jdbc-3.2.0.RELEASE.jar

spring-tx-3.2.0.RELEASE.jar

三、在项目中添加配置文件与属性文件

1.在项目中创建conf目录

2.在conf目录下添加属性文件

属性文件名称:jdbc.properties

属性文件内容:

jdbc.url=jdbc:mysql://localhost:3306/spring

jdbc.driver=com.mysql.jdbc.Driver

jdbc.username=root

jdbc.password=root

2.在conf目录下添加spring核心配置文件

配置文件名称:applicationContext.xml

配置文件内容:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

四、实现bean设计

1.在src目录下创建实体bean的包

包名:cn.jbit.spring101310.domain

2.在包下创建实体bean

public class Account {

private Integer id;

private String name;

private Double money;

//省略get and set

}

五、设计Dao层

1.在src目录下创建dao层的包

包名:cn.jbit.spring101310.dao

2.在包下创建dao层的接口与实现类

1)接口设计

接口名称:IAccountDao.java

接口内容:

public interface IAccountDao {

/**

* 转出

*/

public void outMoney(Account outaccount);

/**

* 转入

*/

public void inMoney(Account inaccount);

}

2)接口实现类设计

实现类名称:AccountDaoImpl.java

实现类内容:

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {

/**

* 转入

*/

@Override

public void inMoney(Account inaccount) {

String sql = "update account set money = money + ? where id = ?";

this.getJdbcTemplate().update(sql,inaccount.getMoney(),inaccount.getId());

}

/**

* 转出

*/

@Override

public void outMoney(Account outaccount) {

String sql = "update account set money = money - ? where id = ?";

this.getJdbcTemplate().update(sql,outaccount.getMoney(),outaccount.getId());

}

}

六、在核心配置文件中配置Dao

七、在核心配置文件中配置事务相关信息

八、业务层设计

1.在src目录下创建业务层的包

包名:cn.jbit.spring101310.service

2.在包下创建业务层的接口与实现类

1)接口设计

接口名称:AccountService.java

接口内容:

public interface AccountService {

public void transfer(Account outAccount,Account inAccount);

}

2)接口实现类设计

实现类名称:AccountServiceImpl.java

实现类内容:

public class AccountServiceImpl implements AccountService {

private IAccountDao accountDao;

private TransactionTemplate transactionTemplate;

@Override

public void transfer(final Account outAccount, final Account inAccount) {

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

@Override

protected void doInTransactionWithoutResult(TransactionStatus arg0) {

//转出

accountDao.outMoney(outAccount);

int a = 1/0;

//转入

accountDao.inMoney(inAccount);

}

});

}

//省略get and set

}

九、在核心配置文件中配置业务层

十、测试

1.在项目上创建test目录

/test

2.在test目录下创建测试包

包名:cn.jbit.spring101310.service

3.在测试包下创建测试类

测试类名:AccountServiceTest.java

测试类的内容:

public class AccountServiceTest {

@Test

public void testTranser(){

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

AccountService accountService = (AccountService) context.getBean("accountService");

Account outAccount = new Account();

outAccount.setId(1);

outAccount.setMoney(500D);

Account inAccount = new Account();

inAccount.setId(2);

inAccount.setMoney(500D);

accountService.transfer(outAccount, inAccount);

}

}

java 编程式事务管理_spring-编程式事务管理相关推荐

  1. spring分布式事务示例_Spring声明式事务示例

    spring分布式事务示例 事务是具有ACID (原子,一致,隔离和持久)属性的工作单元. 原子意味着所有更改都发生或什么都没有发生. 如果从一个帐户借钱并记入另一个帐户,则交易将确保借记和贷项均已完 ...

  2. dubbo分布式事务解决方案_spring boot 分布式事务解决方案

    点击上方蓝色字体,选择"标星公众号" 优质文章,第一时间送达 上一篇:这300G的Java资料是我师傅当年给我的,免费分享给大家 下一篇:这200G的Java实战资料是我师傅当年教 ...

  3. java编程式事务_Spring编程式和声明式事务实例讲解

    Spring事务管理 Spring支持两种方式的事务管理: 编程式事务管理: 通过Transaction Template手动管理事务,实际应用中很少使用, 使用XML配置声明式事务: 推荐使用(代码 ...

  4. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

  5. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  6. 全面分析 Spring 的编程式事务管理及声明式事务管理

    转自:http://www.open-open.com/lib/view/open1414310646012.html 关于本教程 本教程将深切讲授 Spring 庞杂而丁壮夜的事务治理功用,包括编程 ...

  7. Spring事务传播行为-编程式事务

    1. 编程式事务 Spring 事务管理有两种方式: 编程式事务:通过编码方式实现事务: 声明式事务:基于AOP实现,如 @Transactional 声明式注解. 2. 实现原理 编程式事务基于Tr ...

  8. sprinboot中编程式事务_SpringBoot系列教程之事务传递属性

    200202-SpringBoot系列教程之事务传递属性 对于mysql而言,关于事务的主要知识点可能几种在隔离级别上:在Spring体系中,使用事务的时候,还有一个知识点事务的传递属性同样重要,本文 ...

  9. java多数据源事务管理_Spring中实现多数据源事务管理 - CSDN博客

    前言 由于项目中引入了多个数据源,并且需要对多个数据源进行写操作,那么多数据源的事务管理自然成了不可避免的问题,这也让我对 @Transactional注解有了进一步的理解(但实际上也并不是非常深入) ...

  10. java spring 事务传播_spring事务传播机制实例讲解

    天温习spring的事务处理机制,总结如下 对于SQL事务的概念以及ACID性质,可以参见我的另一篇博文 http://kingj.iteye.com/admin/blogs/1675011 spri ...

最新文章

  1. linux下多线程的调试
  2. 『第26天』Sunos (一)
  3. PhpCms V9调用指定栏目子栏目文章的两种方法
  4. Yelp研发实践:使用服务拆分单块应用
  5. 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目
  6. Appcan开发笔记:导出Excel文件
  7. SpringBoot高级-消息-RabbitTemplate发送接受消息序列化机制
  8. oracle导入 表 卡住了,oracle数据库怎么导入dmp,只导入数据不导入表结构?
  9. python prettytable格式设置_Python prettytable的使用方法
  10. truncate table(截断表)
  11. java json日期格式化_JSON格式化日期方法
  12. 千锋培训python好吗?靠谱吗?
  13. 美团-外卖骑手背后的AI技术
  14. 移动互联网需求革命:由“生理需求”到“自我实现”
  15. 安卓中dumpsys命令使用
  16. 日本SUPER DELIVERY电商使用虚拟信用卡海淘购物攻略
  17. NEON优化:软件性能优化、降功耗怎么搞?
  18. java程序框图 质数_判断质数的程序框图和算法
  19. css3运动框架,模拟现实物体运动的js动画库框架-Anima
  20. mysql max_allowed_packet 到底什么意思

热门文章

  1. 投资学U06 风险资产配置 习题笔记
  2. 转贴(电脑报):VBA开发实用指南
  3. 分布式系列之分布式存储ceph初识
  4. 【Bug】steam双方都是国区 礼物无法入库问题
  5. 工信部计算机二级证书有什么用,公务员考试,这三个证书用处大,持证年薪10W+...
  6. 爬取微信朋友圈信息-可视化
  7. Android之集成极光推送
  8. SIFT特征提取与检测
  9. Java获取单个字符的方法
  10. 往日不忆,来日可追-你好,2019!