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

准备工作:

    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. Java匿名内部类的学习

    新建一个抽象类或者接口,抽象类中只要有一个抽象方法就是抽象类,接口的定义是:里面的方法全部都是抽象方法,接口和抽象类不能直接实例化,需要子类来实现 /* 匿名内部类: 1.匿名内部类其实就是内部类的简 ...

  2. vs启动项目提示Web 服务器被配置为不列出此目录的内容。

    解决方法 确认网站或应用程序配置文件中的 configuration/system.webServer/directoryBrowse@enabled 属性已设置为 true. 配置一下web.con ...

  3. JS 位数不够自动左补0

    var mycode = "11"; mycode = (Array(4).join(0) + parseInt(mycode)).slice(-4);//0011 mycode1 ...

  4. 二分图 and code1170 双栈排序

    6.6二分图 二分图是这样一个图: 有两顶点集且图中每条边的的两个顶点分别位于两个顶点集中,每个顶点集中没有边直接相连接. 无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶 ...

  5. Sketch插件新利器——使用Mockplus DS制作设计规范

    Sketch,作为一款专为图标和界面设计而打造的优质矢量绘图工具,也是设计师们制作和完善公司企业内部设计规范系统不可或缺的设计工具. 然而,逐个导出和上传Sketch编辑优化的设计系统资源费时而费力. ...

  6. c# windows服务的制作

    一.创建一个Windows Service )创建Windows Service项目 )对Service重命名 将Service1重命名为你服务名称,这里我们命名为Servicetest. 二.创建服 ...

  7. winscp 使用root身份登录

    参考: https://www.haiyun.me/archives/winscp-sftp-sudo-root.html 一般root账户在服务器上会被禁止ssh,此时普通用户通过sudo执行管理员 ...

  8. numpy中tile()函数

    函数形式: tile(A,rep) 功能:重复A的各个维度 参数类型: - A: Array类的都可以,即A是一个ndarry数组- rep:A沿着各个维度重复的次数,表示变成的矩阵的形状,例如rep ...

  9. 访问localhost文件下的testmysql.php文件报Not Found

    但是访问localhost:8081/index.php没有报该错误,页面显示success,并没有显示wamp的主页 出错原因:Apache和php没关联好,修改一下Apache的httpd.con ...

  10. raw格式转换成qcow2格式

    qemu-img convert -f raw redhat6.7-kvm-postgresql9.2.3-8disk-pulsar2.2 -O qcow2 redhat6.7-kvm-postgre ...