spring框架 事务 xml配置方式
user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15
maxPoolSize=25
minPoolSize=5
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
</bean> <!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="amountDaoImpl" class="com.zr.trasaction.AmountDaoImpl.AmountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="amountServiceImpl" class="com.zr.trasaction.AmountServiceImpl.AmountServiceImpl">
<property name="amountDaoImpl" ref="amountDaoImpl"></property>
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的属性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transferMoneyService" propagation="REQUIRED"/>
<!-- read-only="true"只是为了数据库维护方便 -->
<tx:method name="transferMoneyService" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面,让切面和事务关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.zr.trasaction.AmountServiceImpl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
</aop:config> </beans>
package com.zr.trasaction.entity;
public class Amount {
private String user;//用户名
private double money;//金额
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Amount() {
super();
}
public Amount(String user, double money) {
super();
this.user = user;
this.money = money;
}
@Override
public String toString() {
return "Amount [user=" + user + ", money=" + money + "]";
}
}
package com.zr.trasaction.entity;
public class BalanceException extends RuntimeException {
private static final long serialVersionUID = 1L;
public BalanceException() {
super();
}
public BalanceException(String message) {
super(message);
}
public BalanceException(String message, Throwable cause) {
super(message, cause);
}
public BalanceException(Throwable cause) {
super(cause);
}
protected BalanceException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
package com.zr.trasaction.AmountDao;
public interface AmountDao {
/**
* 转出钱
* @return
*/
public boolean decreaseMoney(String username,double money) ;
/**
* 转入钱
* @return
*/
public boolean increaseMoney(String username,double money);
}
package com.zr.trasaction.AmountDaoImpl;
import org.springframework.jdbc.core.JdbcTemplate;
import com.zr.trasaction.AmountDao.AmountDao;
public class AmountDaoImpl implements AmountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public boolean decreaseMoney(String username, double money) {
String sql = "UPDATE TEST SET MONEY=MONEY-? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
}
@Override
public boolean increaseMoney(String username, double money) {
String sql = "UPDATE TEST SET MONEY=MONEY+? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
}
}
package com.zr.trasaction.AmountService;
import com.zr.trasaction.entity.Amount;
public interface AmountService {
/**
* 转账业务
* @param desAmount 扣钱的账户
* @param incAmount 增加的账户
* @param money 转账的金额
* @return
*/
public boolean transferMoneyService(Amount desAmount,Amount incAmount,double money);
}
package com.zr.trasaction.AmountServiceImpl; import org.springframework.jdbc.core.JdbcTemplate; import com.zr.trasaction.AmountDaoImpl.AmountDaoImpl;
import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount;
import com.zr.trasaction.entity.BalanceException; public class AmountServiceImpl implements AmountService { private AmountDaoImpl amountDaoImpl;
private JdbcTemplate jdbcTemplate; public void setAmountDaoImpl(AmountDaoImpl amountDaoImpl) {
this.amountDaoImpl = amountDaoImpl;
} public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} @Override
public boolean transferMoneyService(Amount desAmount, Amount incAmount,
double money) {
String sql = "SELECT MONEY FROM TEST WHERE USERNAME=?";
double balance = jdbcTemplate.queryForObject(sql, Double.class, desAmount.getUser()); boolean success1 = amountDaoImpl.decreaseMoney(desAmount.getUser(), money);
boolean success2 = amountDaoImpl.increaseMoney(incAmount.getUser(), money); if(balance < money){
throw new BalanceException("余额不足");
}
return success1 && success2;
} }
package com.zr.trasaction.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount; public class TestA { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Amount desAmount = new Amount("lf", 0);
Amount incAmount = new Amount("tl", 0);
AmountService impl = (AmountService)ac.getBean("amountServiceImpl");
boolean isSuccess = impl.transferMoneyService(desAmount, incAmount, 10);
System.out.println("==:"+isSuccess);
} }
spring框架 事务 xml配置方式的更多相关文章
- spring框架 事务 注解配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- Spring MVC 的 XML 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...
- Spring 中使用XML配置方式和使用注解方式实现DI
Spring容器给我们提供了很好的环境,我们只关注主要业务即可,其他的无需关注太多.今天刚学的DI DI(Dependency Injection):依赖注入 使用XML配置文件完成依赖注入 1.1普 ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring框架(2)---IOC装配Bean(xml配置方式)
IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- Spring之AOP原理、代码、使用详解(XML配置方式)
Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...
- Spring框架事务支持模型的优势
全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...
随机推荐
- WC2019 T1 数树
WC2019 T1 数树 传送门(https://loj.ac/problem/2983) Question 0 对于给定的两棵树,设记两颗树 \(A,B\) 的重边数量为 \(R(A,B)\),那么 ...
- CSS设计取消a标签的修饰,转为文本
用法:text-decoration:none;修饰:text-decoration的用法:http://www.runoob.com/cssref/pr-text-text-decoration.h ...
- mysql之 explain、optimizer_trace 执行计划
一.explain mysql> explain select host,user,plugin from user ;+----+-------------+-------+------+-- ...
- 洛谷 2680 (NOIp2015) 运输计划
题目:https://www.luogu.org/problemnew/show/P2680 因为是最长的时间最短,所以二分! 离线LCA可以知道路径长度.每次只看超过二分值的路径. 原本的想法是遍历 ...
- (装)Android杂谈--禁止TimePicker控件通过keyboard输入
Android 4.1版本以上用的是类似与ios的滚动时间控件,但是4.1以下,用的TimePicker确实通过点击上下按钮来更改时间的,虽然也提供了编辑框编辑,但是可能会超出编辑范围 如果要禁止编辑 ...
- WS-* 协议
Web服务作为实现SOA中服务的最主要手段.跟Web Service相关的标准,它们大多以“WS-”作为名字的前缀,所以统称WS-*. Web服务最基本的协议包括UDDI,WSDL和SOAP,通过它们 ...
- 第17篇 shell编程基础(2)
shell study 1.Exit StatusIf the command executed successfully (or true), the value of $? is zero. If ...
- spring-aop + memcached 的简单实现
一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...
- PhantomJS 一个隐形的浏览器
下载地址: http://phantomjs.org/download.html 使用方法: 下载压缩包解压出来找到phantomjs.exe 放到python的根目录下
- java代码做repeat次运算,从键盘输入几个数,比最值
总结:今天这个题目有点灵活,因为它不但要求输出结果,还要进行几次相同的输入,不退出循环 import java.util.Scanner; //从键盘一次输入更多的数,然后把每一次的数进行---可比较 ...