Java框架spring学习笔记(十七):事务操作
2017-11-10 15:44
363 查看
事务操作创建service和dao类,完成注入关系
service层叫业务逻辑层
dao层单纯对数据库操作层,在dao层不添加业务
假设现在有一个转账的需求,狗蛋有10000元,建国有20000元,狗蛋向建国转账1000元钱。
编写service层创建业务逻辑,OrderService.java
编写dao层进行数据库操作,OrderDao.java
编写配置文件bean.xml
编写测试文件TestService.java
文件结构
运行之后
数据库内容发生变化,完成转账
service层叫业务逻辑层
dao层单纯对数据库操作层,在dao层不添加业务
假设现在有一个转账的需求,狗蛋有10000元,建国有20000元,狗蛋向建国转账1000元钱。
编写service层创建业务逻辑,OrderService.java
1 import cn.dao.OrderDao; 2 3 public class OrderService { 4 private OrderDao orderDao; 5 6 public void setOrderDao(OrderDao orderDao) { 7 this.orderDao = orderDao; 8 } 9 10 //调用dao的方法 11 //业务逻辑层,写转账业务 12 public void accountMoney(){ 13 //狗蛋转账给建国,在账面上看就是狗蛋减钱,建国多钱 14 //狗蛋减钱 15 orderDao.lessMoney(); 16 //建国多钱 17 orderDao.moreMoney(); 18 } 19 }
编写dao层进行数据库操作,OrderDao.java
1 package cn.dao; 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 5 public class OrderDao { 6 //注入jdbcTemplate 7 private JdbcTemplate jdbcTemplate; 8 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 9 this.jdbcTemplate = jdbcTemplate; 10 } 11 12 /** 13 * 对数据库操作,不做业务操作 14 */ 15 //狗蛋减钱的方法 16 public void lessMoney(){ 17 String sql = "update account set salary=salary-? where username=?"; 18 jdbcTemplate.update(sql,1000,"狗蛋"); 19 } 20 //建国加钱的方法 21 public void moreMoney(){ 22 String sql = "update account set salary=salary+? where username=?"; 23 jdbcTemplate.update(sql,1000,"建国"); 24 } 25 }
编写配置文件bean.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop.xsd "> 16 17 <!-- 配置c3p0连接池 --> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <!-- 注入dao对象 --> 20 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 21 <property name="jdbcUrl" value="jdbc:mysql:///test"></property> 22 <property name="user" value="root"></property> 23 <property name="password" value="jqbjqbjqb123"></property> 24 </bean> 25 26 <bean id="orderService" class="cn.service.OrderService"> 27 <property name="orderDao" ref="orderDao"></property> 28 </bean> 29 <bean id="orderDao" class="cn.dao.OrderDao"> 30 <!-- 注入jdbcTemplate对象--> 31 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 32 </bean> 33 34 <!-- 创建jdbcTemplate对象 --> 35 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 36 <!-- 把dataSource传递到模板对象中--> 37 <property name="dataSource" ref="dataSource"></property> 38 </bean> 39 40 </beans>
编写测试文件TestService.java
1 package cn.test; 2 import cn.service.OrderService; 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class TestService { 8 @Test 9 public void testDemo(){ 10 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 11 OrderService orderService = (OrderService) context.getBean("orderService"); 12 orderService.accountMoney(); 13 } 14 }
文件结构
运行之后
数据库内容发生变化,完成转账
相关文章推荐
- Redis源代码分析(十七)--- multi事务操作
- Redis源码分析(十七)--- multi事务操作
- 删除信息[置顶] Java中怎样实现批量删除操作(Java对数据库进行事务处理)?
- SQL Server 2012完全备份、差异备份、事务日志备份和还原操作;
- iOS-FMDB事务操作SQLite数据库
- 对数据进行操作——事务Transaction
- 事务处理 多步操作回滚
- Ibatis的批量操作和事务管理。
- 数据库操作 —— 快速入门:基本配置和使用、读写分离 & 数据库事务
- SQL Server事务日志的几个常用操作
- StackExchange.Redis 事务操作
- Maven构建SpringMVC项目实现注解、数据库事务管理及存储过程操作
- 程序使用事务操作oracle多表的方法案例
- 自定义支持事务操作的资源管理器
- 数据库事务操作
- Android数据库事务操作
- 配置Hive支持事务——让hive支持行级insert、update、delete操作
- 事务提交操作遇到的问题
- mysqli的多条sql语句操作以及简单的事务处理~~~~~
- Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务