事务管理--------使用spring的transactionManager管理事务

本文介绍了如何在Spring中利用transactionManager进行事务管理,包括在dao层、service层的配置以及测试类的使用,特别强调了注解方式的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

事务管理--------使用spring的transactionManager管理事务

dao层

public class EmpDaoImpl extends JdbcDaoSupport implements EmpDao {
    
    @Override
    public Emp selectOneEmp(Integer id) throws SQLException {
        String sql = "select * from emp where empno=?";
        List<Emp> emps = getJdbcTemplate().query(sql, new RowMapper<Emp>() {
            @Override
            public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
                Emp emp = new Emp();
                emp.setEname(resultSet.getString("ename"));
                emp.setEmpno(resultSet.getInt("empno"));
                emp.setJob(resultSet.getString("job"));
                emp.setHiredate(resultSet.getDate("hiredate"));
                emp.setSal(resultSet.getDouble("sal"));
                return emp;
            }
        },id);
        return emps.get(0);
    }
}

service层

public class EmpServiceImpl implements EmpService {
    
    @Override
    public Emp selectOneEmp(Integer id) throws SQLException {
        return empDao.selectOneEmp(id);
    }
    
    @Override
    public void sqlChange(Integer src,Integer target) {
        try {
            Emp srcEmp = empDao.selectOneEmp(src);
            srcEmp.setSal(srcEmp.getSal()-500);
            empDao.updateEmp(srcEmp);
            int i = 1/0;
            Emp targetEmp = empDao.selectOneEmp(target);
            targetEmp.setSal(targetEmp.getSal()+500);
            empDao.updateEmp(targetEmp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

spring配置

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.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">
    <!--    配置数据源 使用druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/lanqiao?characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="empDao" class="org.lanqiao.dao.impl.EmpDaoImpl">
        <property name="dataSource" ref="dataSource"/>
     </bean>

    <bean id="empService" class="org.lanqiao.service.impl.EmpServiceImpl">
        <property name="empDao" ref="empDao"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
	<!--事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
        <tx:attributes>
            <tx:method name="*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
       </tx:attributes>
    </tx:advice>
    <!--使用aop的方式将事务织入-->
	<aop:config>
        <!--配置切入点-->
        <aop:pointcut id="empPoint" expression="execution(* org.lanqiao.service..*.*(..))"/>
		<!--配置顾问 = 通知 + 切入点-->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="empPoint"/>
	</aop:config>
</beans>

测试类


@Test
public void sqlChangeTest() throws SQLException {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    EmpService service = context.getBean("empService",EmpService.class);
    service.sqlChange(1001,1002);
}

注解方式

@Service("empService")
//使用在类上,表示当前类中的所有方法的将使用该策略
@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
public class EmpServiceImpl implements EmpService {
    
     @Override
    //使用在方法上,表示当前方法将使用改事务策略
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Emp selectOneEmp(Integer id) throws SQLException {
        return empDao.selectOneEmp(id);
    }
    @Override
    public void sqlChange(Integer src,Integer target) {
        try {
            Emp srcEmp = empDao.selectOneEmp(src);
            srcEmp.setSal(srcEmp.getSal()-500);
            empDao.updateEmp(srcEmp);
            int i = 1/0;
            Emp targetEmp = empDao.selectOneEmp(target);
            targetEmp.setSal(targetEmp.getSal()+500);
            empDao.updateEmp(targetEmp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.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">
    <context:annotation-config/>
    <context:component-scan base-package="org.lanqiao"/>
	<!--默认的事务管理器的bean name是transactionManager-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--    配置数据源 使用druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/lanqiao?characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--dao继承了JdbcDaoSupport 父类中存在dataSource只能用setter方式注入-->
    <bean id="empDao" class="org.lanqiao.dao.impl.EmpDaoImpl">
        <property name="dataSource" ref="dataSource"/>
     </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值