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

准备工作:

    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测试类:

    

 package test;

 import com.hxzy.spring.c3p0.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserService service = (UserService) context.getBean("userService");
service.test_();
}
}

   spring-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!--============创建c3p0链接池=========-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/user_transfer"></property>
<property name="user" value="root"></property>
<property name="password" value="gubin"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<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"></tx:annotation-driven>
<!--============开启扫包==========-->
<context:component-scan base-package="com.hxzy.spring"></context:component-scan>
</beans>

  数据库:

    

    

      

Spring中 使用注解+c3p0+事物 《模拟银行转账》的更多相关文章

  1. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  2. Spring中Value注解的使用

    Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...

  3. 全面解析Spring中@ModelAttribute注解的用法

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...

  4. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  5. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

  6. Spring中使用注解时启用<context:component-scan/>

    在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <conte ...

  7. Spring中异步注解@Async的使用、原理及使用时可能导致的问题

    前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...

  8. Spring中@Import注解的使用

    Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...

  9. Spring中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...

随机推荐

  1. 跟我学算法-图像识别之图像分类(下)(GoogleNet网络, ResNet残差网络, ResNext网络, CNN设计准则)

    1.GoogleNet 网络: Inception V1 - Inception V2 - Inception V3 - Inception V4 1. Inception v1 split - me ...

  2. vim调整粘贴时的文本缩进

    解决办法: 1. 在拷贝前输入:set paste (这样的话,vim就不会启动自动缩进,而只是纯拷贝粘贴)2. 拷贝完成之后,输入:set nopaste (关闭paste) 在 Vim 中粘贴文本 ...

  3. docker-compose搭建单机多节点es + kibana

    docker-compose.yml配置如下: version: '2.2' services: elasticsearch: image: docker.elastic.co/elasticsear ...

  4. 禁止ImageCapture自动启动

    [禁止ImageCapture自动启动] 打开ImageCapture,点开左下角菜单,把Connecting this iPhone opens:的内容改为以下选项即可.

  5. torque

    torque - 必应词典 美[tɔrk]英[tɔː(r)k] n.(使机器等旋转的)转矩 网络扭矩:扭力:力矩 变形过去分词:torqued:现在分词:torquing:第三人称单数:torques ...

  6. shader一般都是用工具调试的

    N卡的话用nvidia的nVidia FX Composer, A卡的话用ATI的render monkey 顶点着色器从何方拿到这些数据?在U3D环境下,答案是从绑定到game object中的Me ...

  7. Cplus Overolad new and delete Operator

    思考:在C++类中,通过设计类的构造和析构函数,就已经把复杂的内存管理起来了. 及时是简单的结构体,也是有构造和析构函数的,而下面这种情况,可以在非结构中使用. /** Operator Overlo ...

  8. MySQL之——GROUP BY分组取字段最大值

    转载自:http://blog.csdn.net/l1028386804/article/details/54657412 假设有一个业务场景,需要查询用户登录记录信息,其中表结构如下: CREATE ...

  9. 数据挖掘中ID3算法实现zz

    id3 function D = ID3(train_features, train_targets, params, region) % Classify using Quinlan's ID3 a ...

  10. mongo学习- group操作

    group可以使用 $sum,$avg,$max,$min,$first,$last