spring学习(十一)—事务管理(注解方式实现)

来源:互联网 发布:繁体系统安装简体软件 编辑:程序博客网 时间:2024/05/21 08:47

转载自传智博客学习视频
声明式事务管理配置步骤:
1.在配置文件中配置事务管理器
2.在配置文件中配置事务注解
3.在要使用事务的方法所在类上面添加注解(该类里面的所有方法都会被事务管理)

1.代码结构
这里写图片描述

2.测试类

package service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestService {    @Test    public void testService(){        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");        OrdersService ordersService = (OrdersService) context.getBean("ordersService");        ordersService.accountMoney();    }}

3.服务类

package service;import org.springframework.transaction.annotation.Transactional;import dao.OrderDao;@Transactionalpublic class OrdersService {    public OrderDao orderDao;    public void setOrderDao(OrderDao orderDao) {        this.orderDao = orderDao;    }    public void accountMoney(){        orderDao.lessMoney();        int i = 10/0;        orderDao.moreMoney();    }}

4.数据库操作类

package dao;import org.springframework.jdbc.core.JdbcTemplate;public class OrderDao {    public JdbcTemplate jdbcTemplate;    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {        this.jdbcTemplate = jdbcTemplate;    }    public void lessMoney() {        String sql="update account set salary=salary+? where username=?";        jdbcTemplate.update(sql,-1000,"xiaowang");    }    public void moreMoney() {        String sql="update account set salary=salary+? where username=?";        jdbcTemplate.update(sql,1000,"xiaoma");    }}

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: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/aop         http://www.springframework.org/schema/aop/spring-aop.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:///springlearn"></property>        <property name="user" value="root"></property>        <property name="password" value="root"></property>    </bean>    <!--第一步 配置事务管理器 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 注入dataSource -->        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- 第二步 开启事务注解 -->    <tx:annotation-driven transaction-manager="transactionManager"/>    <bean id="ordersService" class="service.OrdersService">        <property name="orderDao" ref="orderDao"></property>    </bean>    <bean id="orderDao" class="dao.OrderDao">        <property name="jdbcTemplate" ref="jdbcTemplate"></property>    </bean>    <!-- 创建jdbc模板对象 -->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"></property>    </bean></beans>
0 0
原创粉丝点击