1. 案例结构
2. 所用数据库表
3. jdbc.properties配置文件
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test?characterEncoding\=utf-8
username=root
password=root
initialSize=5
maxActive=50
maxIdle=10
minIdle=5
4. 1 applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- 启用注解 -->
<context:annotation-config/>
<!--扫描生成单例实体对象 -->
<context:component-scan base-package="com.zhq.*"/>
<!-- 用于加载连接数据库的属性文件 -->
<bean id="preferencesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"/>
</bean>
<!--配置数据源,创建与数据库的连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!--连接池配置 -->
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean>
<!--采用Spring提供的Jdbc对象,可以操作数据 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--配置数据源对象 -->
<constructor-arg ref="dataSource"/>
</bean>
<!--事务的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--事务的处理要依赖现有的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.2
applicationContext.xml 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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- 启用注解 -->
<context:annotation-config/>
<!--扫描生成单例实体对象 -->
<context:component-scan base-package="com.zhq.*"/>
<!-- 用于加载连接数据库的属性文件 -->
<bean id="preferencesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"/>
</bean>
<!--配置数据源,创建与数据库的连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!--连接池配置 -->
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean>
<!--采用Spring提供的Jdbc对象,可以操作数据 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--配置数据源对象 -->
<constructor-arg ref="dataSource"/>
</bean>
<!--启用事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--事务的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--事务的处理要依赖现有的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--用XML来做事务的配置 -->
<aop:config>
<!--切入点 -->
<aop:pointcut expression="execution(* com.zhq.service.*.*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!--事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--事务的传播行为 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
5. pojo代码
package com.zhq.pojo;
import org.springframework.stereotype.Repository;
@Repository
public class Bank {
private int account;
private double money;
public int getAccount() {
return account;
}
public void setAccount(int account) {
this.account = account;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Bank [account=" + account + ", money=" + money + "]";
}
}
6. dao代码
package com.zhq.dao;
public interface BankDao {
//根据账号查询余额
public double getMoneyById(String account);
//根据账号存钱
public void addMoney(String account,double money);
//根据账号取钱
public void subMoney(String account,double money);
}
package com.zhq.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.zhq.util.MyException;
@Repository
public class BankDaoImpl implements BankDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public double getMoneyById(String account) {
String sql="select money from bank where account=?";
return jdbcTemplate.queryForObject(sql, new Object[] {account}, double.class);
}
@Override
public void addMoney(String account, double money) {
String sql="update bank set money=money+? where account=?";
jdbcTemplate.update(sql, money,account);
}
@Override
public void subMoney(String account, double money) {
double num=getMoneyById(account);//查询余额
if(num>money) {//余额充足,可以取钱
String sql="update bank set money=money-? where account=?";
jdbcTemplate.update(sql, money,account);
}else {//余额不足,向外抛出异常
throw new MyException("余额不足");
}
}
}
7. service 代码
package com.zhq.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zhq.dao.BankDao;
@Repository
public class BankService {
@Autowired
BankDao dao;
@Transactional//如果是XML方式@Transactional不需要
public void transferAccounts(String inAccount,String outAccount,double money) {
dao.addMoney(inAccount, money);
dao.subMoney(outAccount, money);
}
}
8. util代码
package com.zhq.util;
/**
* 自定义异常类
* */
public class MyException extends RuntimeException {
public MyException(String msg) {
super(msg);
}
}
9. controller 代码
package com.zhq.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhq.service.BankService;
public class Main {
public static void main(String[] args) {
ApplicationContext cxt=new ClassPathXmlApplicationContext("config/applicationContext.xml");
BankService ser=(BankService) cxt.getBean(BankService.class);
try {
ser.transferAccounts("10001", "10002", 500);
System.out.println("转账成功!");
} catch (Exception e) {
System.out.println(e.getMessage()+",转账失败!");
}
}
}
10. 运行效果