• Spring中 使用注解+c3p0+事物 《模拟银行转账》


      使用注解的方式  模拟转账 要么都成功 要么都失败 !保持一致性!

    准备工作:

        jar包: 

            

        

      需要的类:

                

          

       UserDao: 

          

    1. package com.hxzy.spring.c3p0.Dao;

      import lombok.Data;
      import org.springframework.jdbc.core.JdbcTemplate;
      import org.springframework.stereotype.Controller;
      import org.springframework.transaction.annotation.Transactional;

      import javax.annotation.Resource;
      @Transactional //开启事务
      @Data //生成set get
      @Controller("userDao") //配置 加入ioc容器
      public class UserDao {
      @Resource(name = "jdbcTemplate")
      private JdbcTemplate template;

      public void test_c3(){
      String sql = "UPDATE user_all SET u_balance = u_balance-5 WHERE uid = 1";
      // int a = 100/0;
      String sql1 = "UPDATE user_all SET u_balance = u_balance+5 WHERE uid = 2";
      template.update(sql);
      template.update(sql1);
      }
      }

      UserService:

        

    1. package com.hxzy.spring.c3p0.Service;

      import com.hxzy.spring.c3p0.Dao.UserDao;
      import lombok.Data;
      import org.springframework.stereotype.Service;

      import javax.annotation.Resource;

      @Data
      @Service("userService")
      public class UserService {
      @Resource(name = "userDao")
      private UserDao userDao ;
      public void test_(){
      userDao.test_c3();
      }
      }

      Test测试类:

        

     1 package test;
     2 
     3 import com.hxzy.spring.c3p0.Service.UserService;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 public class Test {
     8     public static void main(String[] args) {
     9         ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    10        UserService service =  (UserService) context.getBean("userService");
    11        service.test_();
    12     }
    13 }

       spring-config.xml:

        

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     6 
     7         <!--============创建c3p0链接池=========-->
     8         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     9             <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    10             <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/user_transfer"></property>
    11             <property name="user" value="root"></property>
    12             <property name="password" value="gubin"></property>
    13         </bean>
    14         <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    15             <property name="dataSource" ref="dataSource"></property>
    16         </bean>
    17         <!--============创建事务==============-->
    18         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    19                 <property name="dataSource" ref="dataSource"></property>
    20         </bean>
    21         <!--开启对事物的支持-->
    22         <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    23         <!--============开启扫包==========-->
    24         <context:component-scan base-package="com.hxzy.spring"></context:component-scan>
    25 </beans>

      数据库:

        

         

          

  • 相关阅读:
    Saiku相关异常处理(十五)
    Saiku登录源码追踪.(十三)
    Saiku调用WS接口(十四)
    Saiku本地编译运行后Debug调试(十二)
    Windows查看Java内存使用情况
    Saiku免登录嵌入其他系统使用(十一)
    Saiku更改源代码实现默认查询一天的数据(十)
    Saiku关于MDX过滤的使用-默认显示最近一周的数据(九)
    Saiku部分函数解析(八)
    Saiku缓存处理(七)
  • 原文地址:https://www.cnblogs.com/gu-bin/p/10481378.html
Copyright © 2020-2023  润新知