转账环境搭建
1、创建数据库表,添加数据
image.png2、创建service和dao完成注入的关系
(1)service:业务逻辑层
(2)dao:单独对数据库操作的层,dao层不添加业务逻辑
配置bean service注入dao dao注入模版
(3)需求:张三给李四转账1000块钱
- 张三少1000块钱
- 李四多1000块钱
3、代码
Service层OrdersService.java
import org.springframework.beans.factory.annotation.Autowired;
import work.zhangdoudou.Dao.OrdersDao;
public class OrdersService {
@Autowired
private OrdersDao ordersDao;
/*
* 调用dao层
* 业务逻辑,写转账业务
*/
public void accountMoney(String name1,String name2,Integer money) {
//张三少1000块钱
ordersDao.lessMoney(name1,money);
//李四多1000快钱
ordersDao.moreMoney(name2,money);
}
}
dao层OrdersDao.java
package work.zhangdoudou.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class OrdersDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/*
* 对数据库操作,不屑业务
*/
//张三少钱的方法
public void lessMoney(String name,Integer money) {
String sql="UPDATE account SET salary=salary-? WHERE u_name=?";
jdbcTemplate.update(sql,money,name);
}
//李四多钱的方法
public void moreMoney(String name,int money) {
String sql="UPDATE account SET salary=salary+? WHERE u_name=?";
jdbcTemplate.update(sql,money,name);
}
}
配置文件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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 开启器注解扫描 -->
<context:component-scan base-package="work.zhangdoudou"></context:component-scan>
<!-- value 从配置文件里面 db.properties中取值 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" ></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="ordersDao" class="work.zhangdoudou.Dao.OrdersDao"></bean>
<bean id="ordersService" class="work.zhangdoudou.Service.OrdersService"></bean>
<!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- dataSource 传递到模板中 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
测试类Test1.java
package work.zhangdoudou.Test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import work.zhangdoudou.Service.OrdersService;
public class Test1 {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
OrdersService ordersService=(OrdersService) context.getBean("ordersService");
ordersService.accountMoney("张三","李四",1000);
}
}
4、运行结果
image.png5、存在的问题
-
如果张三少了1000元后,出现异常,李四不会多1000元,钱丢失了
异常代码
异常代码
运行结果
运行结果
6、解决问题
- 添加事务解决,如果出现异常进行回滚操作
网友评论