spring配置事务之xml方式

创建两张表:

#账户表
create table account
(
   accountid varchar(18) primary key,   #账号id
   balance   double(10,2)                #余额
)

#存款表
create table inaccount
(
  accountid  varchar(18),
  inbalance   double(10,2)    #存入金额
)

#存款
    # 插入存款的数据到inaccount表
    # 查询该账号对应的余额   --account表查
    # 计算新的账号余额
    # 更新账号余额(以账号为id更新account表的balance)
    

Domain:


Account

public class Account {
	private String accountid;

	private Double balance;

	public String getAccountid() {
		return accountid;
	}

	public void setAccountid(String accountid) {
		this.accountid = accountid;
	}

	public Double getBalance() {
		return balance;
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

}

InAccount

public class InAccount {
	private String accountid;
	private Double inbalance;

	public String getAccountid() {
		return accountid;
	}

	public void setAccountid(String accountid) {
		this.accountid = accountid;
	}

	public Double getInbalance() {
		return inbalance;
	}

	public void setInbalance(Double inbalance) {
		this.inbalance = inbalance;
	}
}


Dao层


AccountDao

public interface AccountDao {
	public Account findAccountById(String accountid);

	public void updateAccount(Account account);

}

InAccountDao

public interface InAccountDao {
	public void saveInAccount(InAccount inAccount);
}


Dao实现层


AccountDaoImpl

public class AccountDaoImpl implements AccountDao {
	private JdbcTemplate  jdbcTemplate;
	
	public Account findAccountById(String accountid) {
		String sql="SELECT accountid,balance FROM  account  WHERE  accountid=?";
		Object[] args={accountid};
		Account account=(Account)this.getJdbcTemplate().queryForObject(sql, args, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				    Account account=new Account();
					account.setAccountid(rs.getString(1));
					account.setBalance(rs.getDouble(2));
					return account;
			}
		});	
		return account;
	}
	/**
	 * 更新账号余额
	 */
	public void updateAccount(Account account) {
		try{
			//组织sql语句
			String sql="UPDATE account SET balance=? WHERE  accountid=?";
			Object[]  args={account.getBalance(),account.getAccountid()};
			int[] argTypes={java.sql.Types.DOUBLE,java.sql.Types.VARCHAR};
			//该方法抛出运行时异常
			this.getJdbcTemplate().update(sql, args, argTypes);	
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Resource(name="jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


}

InAccountDaoImpl

public class InAccountDaoImpl implements InAccountDao {
	private JdbcTemplate  jdbcTemplate;
	
	/**
	 * 保存信息到存款表中
	 */
	public void saveInAccount(InAccount inAccount) {
		String sql="INSERT INTO inaccount (accountid, inbalance)  VALUES(?,?)";
		Object[] args={inAccount.getAccountid(),inAccount.getInbalance()};
		int[] argTypes={java.sql.Types.VARCHAR,java.sql.Types.DOUBLE};
		this.getJdbcTemplate().update(sql, args, argTypes);	
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	@Resource(name="jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}

Service层


InAccountService

public interface InAccountService {
	public void saveInAccount(InAccount inAccount);
}

InAccountServiceImpl

public class InAccountServiceImpl implements InAccountService {
	private InAccountDao inAccountDao;
	private AccountDao accountDao;

	public void saveInAccount(InAccount inAccount) {

		// 1* 插入存款的数据到inaccount表
		inAccountDao.saveInAccount(inAccount);
		// 2* 查询该账号对应的余额 --account表查
		Account account = accountDao.findAccountById(inAccount.getAccountid());

		// 3* 计算新的账号余额
		Double balance = account.getBalance() + inAccount.getInbalance();

		account.setBalance(balance);

		// 4* 更新账号余额(以账号为id更新account表的balance)
		accountDao.updateAccount(account);

	}

	public InAccountDao getInAccountDao() {
		return inAccountDao;
	}

	public void setInAccountDao(InAccountDao inAccountDao) {
		this.inAccountDao = inAccountDao;
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

}

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:tx="http://www.springframework.org/schema/tx"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
							http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
							http://www.springframework.org/schema/tx 
							http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
							http://www.springframework.org/schema/aop 
							http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	
	<!-- 
		Spring中加入事务管理
		引入tx命名空间
			xmlns:tx="http://www.springframework.org/schema/tx"
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	 -->
	 
	 <!-- 1.配置DBCP数据源 -->
		<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
			<property name="url" value="jdbc:mysql://localhost:3306/test"/>
			<property name="username" value="root"/>
			<property name="password" value="root"/>
			<!-- 配置初始化连接数 -->
			<property name="initialSize" value="5"/>
			<!-- 配置最大连接数 -->
			<property name="maxActive" value="20"/>
			<!-- 配置最大空闲数,防止洪峰退去时,连接池中连接数过多 -->
			<property name="maxIdle" value="10"/>
			<!-- 配置最小空闲数,防止洪峰到来时,连接池中连接数过少-->
			<property name="minIdle" value="5"/>
			<!-- 设置最大等待事件,如果超过这个时间,连接池将抛出异常,例如:最大连接数是20个,当21个连接来的时候就需要等待-->
			<property name="maxWait" value="5000"/>
		</bean>   
	 
	 <!-- 2.配置jdbcTemplate模板 -->
	 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	 	<property name="dataSource" ref="dbcpDataSource"></property>
	 </bean>
	 
	 <!-- 3.配置事务管理器,AOP的切面 -->
	 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<!-- 注入数据源 -->
	 	<property name="dataSource" ref="dbcpDataSource"/>
	 </bean>
	 
	 <!-- 4.配置AOP的通知,由于通知都是放在一个切面里的,所以这里要声明这个通知属于哪个切面,相当于对切入点的细化,更详细指定某些方法的事务隔离级别和事务传播特性 -->
	 <tx:advice id="advice" transaction-manager="txManager">
	 	<tx:attributes>
	 	 <!-- tx:method属性的设置
             * name必须的 与事务属性关联的方法名。
                 	     通配符(*)可以用来指定一批关联到相同的事务属性的方法。
                 	     如:'get*'、'handle*'、'on*Event'等等。 
             * propagation: 不是必须的,默认值REQUIRED  事务传播行为(7种)
             * isolation:不是必须的  默认值DEFAULT(使用数据库当前默认的隔离级别) 事务隔离级别
             * read-only:不是必须的 默认值false(不是只读的) 事务是否只读?   当save和update方法时,事务如果只读就不合适了
             
             * rollback-for=""  no-rollback-for="" 回滚处理  默认情况下任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
           -->
	 		<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" />
	 		<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" />
	 		<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" />
	 	</tx:attributes>
	 </tx:advice>
	 
	 <!-- 5.配置切入点 -->
	 <aop:config>
	 	<!-- 该切入点应用在类级别(虽然说指定到了方法但是方法是任意,那么就定义到类级别)    com.xxc.service..*.*  两个点表示这个包下及其子包下的*类   如果是一个点就表示service包下的类 -->
	 	<aop:pointcut expression="execution( * com.xxc.service..*.*(..))" id="peform"/>
	 	<!-- 建立切入点和通知的关系 -->
	 	<aop:advisor advice-ref="advice" pointcut-ref="peform"/>
	 </aop:config>
	 
	  <!-- 配置dao层 -->
	  <bean id="accountDao" class="com.xxc.dao.Impl.AccountDaoImpl">
	 	<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	 </bean>
	 <bean id="inAccountDao" class="com.xxc.dao.Impl.InAccountDaoImpl">
	 	<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	 </bean>
	 
	 <!-- 配置业务层 -->
	 <bean id="inAccountService" class="com.xxc.service.impl.InAccountServiceImpl">
	 	<property name="accountDao" ref="accountDao"></property>
	 	<property name="inAccountDao" ref="inAccountDao"></property>
	 </bean>
	 
</beans>

测试类:

public class App {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/applicationContext.xml");
    	
	 	//<bean id="inAccountService" class="cn.itcast.service.impl.InAccountServiceImpl">
        InAccountService inAccountService=(InAccountService)ac.getBean("inAccountService");
        
		//存款
		InAccount inAccount=new InAccount();
		inAccount.setAccountid("1");
		inAccount.setInbalance(2000d);
		inAccountService.saveInAccount(inAccount);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值