zoukankan      html  css  js  c++  java
  • Spring入门之通过注解 处理 数据库事务

    用Spring 中的事务写的银行转帐的例子:(环境同上一个贴子)

    一、表结构: (create table (id int,username varchar(10),salary int);)

    二、文件列表:

    OrdersDao.java  OrdersService Test.java 和beans2.xml

    (要点):1、要在需要处理事务的类前面加上 @Transactional ,表示引入事务注解

                        

                2、在配置 文件 中,配置c3p0连接池,配置dataSource ,通过配置文件创建事务管理器对象,开启事务注解

     1     <!-- 1 配置c3p0连接池 -->
     2      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     3          <property name="DriverClass" value ="com.mysql.jdbc.Driver"></property>
     4          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user"></property>
     5          <property name="user" value="root"></property>
     6          <property name="password" value="123456"></property>
     7      </bean>        
     8      <!-- 2 配置事务管理器 -->
     9      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    10      <!-- 注入dataSource -->
    11          <property name="dataSource" ref="dataSource"></property>
    12      </bean>
    13     <!-- 3、开启配置事务注解 -->
    14     <tx:annotation-driven transaction-manager="transactionManager"/>

     全部代码如下:

    OrdersDao.java

     1 package spring.dao;
     2 
     3 import org.springframework.jdbc.core.JdbcTemplate;
     4 
     5 public class OrdersDao {
     6     private JdbcTemplate jdbcTemplate;
     7 
     8     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
     9         this.jdbcTemplate = jdbcTemplate;
    10     }
    11     
    12     public void lessMoney() {
    13         String sql = "update salary set salary=salary - ? where username=?";
    14         jdbcTemplate.update(sql,1000,"zhangs");
    15     }
    16     public void moreMoney() {
    17         String sql = "update salary set salary=salary + ? where username=?";
    18         jdbcTemplate.update(sql,1000,"wangs");
    19     }
    20     
    21 }
    22 /*
    23  *对数据库操作的方法,不写业务操作; 
    24  */
    View Code

     OrdersService.java

     1 package spring.service;
     2 
     3 import org.springframework.transaction.annotation.Transactional;
     4 
     5 import spring.dao.OrdersDao;
     6 
     7 
     8 @Transactional
     9 public class OrdersService {
    10     private OrdersDao ordersDao;
    11 
    12     public void setOrdersDao(OrdersDao ordersDao) {
    13         this.ordersDao = ordersDao;
    14     }
    15     public void acount() {
    16         //调用DAO的方法
    17         //业务逻辑层,写转帐业务
    18         //zhangs 少1000
    19         
    20         //wangs多1000
    21         ordersDao.moreMoney();
    22         int s = 10/0;
    23         ordersDao.lessMoney();
    24     }
    25 }
    View Code
    TestApi.java
     1 package spring.service;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7     public class TettApi {
     8         @Test
     9         public void testDemo() {
    10             ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
    11             OrdersService ordersService = (OrdersService) context.getBean("ordersService");
    12             ordersService.acount();
    13         }
    14 
    15     }
    View Code
     1 <?xml version="1.0" encoding="utf-8"?>
     2 
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5         xmlns:context="http://www.springframework.org/schema/context"
     6         xmlns:aop="http://www.springframework.org/schema/aop"
     7         xmlns:tx="http://www.springframework.org/schema/tx"
     8         xsi:schemaLocation="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/aop
    13         http://www.springframework.org/schema/aop/spring-aop.xsd
    14         http://www.springframework.org/schema/tx
    15         http://www.springframework.org/schema/tx/spring-tx.xsd">
    16         
    17     <!-- 1配置c3p0连接池 -->
    18      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    19          <property name="DriverClass" value ="com.mysql.jdbc.Driver"></property>
    20          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user"></property>
    21          <property name="user" value="root"></property>
    22          <property name="password" value="123456"></property>
    23      </bean>        
    24      <!-- 2 配置事务管理器 -->
    25      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    26      <!-- 注入dataSource -->
    27          <property name="dataSource" ref="dataSource"></property>
    28      </bean>
    29     <!-- 3、开启配置事务注解 -->
    30     <tx:annotation-driven transaction-manager="transactionManager"/>
    31    
    32     <bean id="ordersDao" class="spring.dao.OrdersDao" >
    33         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    34     </bean>
    35     
    36     <bean id="ordersService" class="spring.service.OrdersService">
    37         <property name="ordersDao" ref="ordersDao"></property>
    38     </bean>
    39     
    40     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    41         <property name="dataSource" ref="dataSource"></property>
    42     </bean>
    43 </beans>
    View Code

     最后一个是beans2.xml配置文件。

    事务一般是成批处理数据库时用到,成组成批执行命令,要不全执行,要么全部不执行。防止有意外事件,造成上面的数减了,下面的数没有加上;(银行转帐例子);

  • 相关阅读:
    HDU 2955 Robberies(01背包)
    HDU 2602 Bone Collector(01背包)
    HUST 1352 Repetitions of Substrings(字符串)
    HUST 1358 Uiwurerirexb jeqvad(模拟解密)
    HUST 1404 Hamming Distance(字符串)
    HDU 4520 小Q系列故事――最佳裁判(STL)
    HDU 2058 The sum problem(枚举)
    【破解】修改程序版权、添加弹窗
    HDU 1407 测试你是否和LTC水平一样高(枚举)
    HDU 1050 Moving Tables(贪心)
  • 原文地址:https://www.cnblogs.com/lrzy/p/8405065.html
Copyright © 2011-2022 走看看