以银行转账为例(本文只作为自己学习spring的备忘录,只对自己负责)
转账的dao层接口
- package com.slowly.springtrancation03;
-
-
-
-
-
-
- public interface accountDao {
-
-
-
-
-
- public void outMoney(String out,Double money);
-
-
-
-
-
-
- public void inMoney(String in,Double money);
- }
转账dao层接口实现类
- package com.slowly.springtrancation03;
-
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
-
- public class accountDaoImp extends JdbcDaoSupport implements accountDao {
-
- @Override
- public void outMoney(String out, Double money) {
- String sql = "update account set balance = balance-? where account_name = ?";
- this.getJdbcTemplate().update(sql, money,out);
-
- }
-
- @Override
- public void inMoney(String in, Double money) {
- String sql = "update account set balance = balance+? where account_name = ?";
- this.getJdbcTemplate().update(sql, money,in);
-
- }
-
- }
转账的业务逻辑层接口
- package com.slowly.springtrancation03;
-
-
-
- public interface accountService {
-
-
-
-
-
-
- public void transfer(String out,String in,Double money);
- }
业务逻辑接口实现类
- package com.slowly.springtrancation03;
-
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallbackWithoutResult;
- import org.springframework.transaction.support.TransactionTemplate;
-
- @Transactional//在需要事务管理的类上添加注解
- public class accountServiceImp implements accountService {
-
- private accountDao accountDao;
-
-
-
- public void setAccountDao(accountDao accountDao) {
- this.accountDao = accountDao;
- }
-
-
-
-
- @Override
- public void transfer(String out, String in, Double money) {
-
- accountDao.outMoney(out, money);
-
- accountDao.inMoney(in, money);
-
- }
-
-
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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
-
-
- <context:property-placeholder location="classpath:jdbc.properties"/>
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${driverClass}"></property>
- <property name="jdbcUrl" value="${jdbcUrl}"></property>
- <property name="user" value="${user}"></property>
- <property name="password" value="${password}"></property>
- </bean>
-
- <bean id="accountService" class="com.slowly.springtrancation03.accountServiceImp">
- <property name="accountDao" ref="accountDao"></property>
- </bean>
-
-
- <bean id="accountDao" class="com.slowly.springtrancation03.accountDaoImp">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
-
- <!-- 注解方式的驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
- </beans>
测试
- package com.slowly.springtrancation03;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class Test {
-
- public static void main(String[] args) {
- ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext3.xml");
- accountService accountService = (com.slowly.springtrancation03.accountService) apt.getBean("accountServiceProxy");
- accountService.transfer("乙", "甲", 600.00);
-
- }
测试用的和上文一样的数据库表
-