问答详情
源自:7-1 课程总结

为什么没有把事务绑定在一起 ,第一个还是修改了

@Service
public class TestServiceImpl implements TestService{
	
	@Resource
	private TestDAO testDAO;
	
	@Override
	@Transactional
	public void sql() {
		Test t1 =  new Test();
			t1.setId(1L);
			t1.setNum(800);
		Test t2 =  new Test();
			t2.setId(2L);
			t2.setNum(1200);
		
		testDAO.update(t1);
		int bug = 1/0;
		testDAO.update(t2);
		
	}

}

<!-- 将事务与mySql关联 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource" />

    </bean>

    

    <!-- 事务注解驱动,标注@Transactional的类和方法将具有事务性 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>


提问者:假象 2015-09-08 16:05

个回答

  • 假象
    2015-09-09 10:40:52

    applicationContext.xml

    <tx:annotation-driven />
    <context:component-scan base-package="com.code" />

    dispatcher-servlet.xml

    <context:component-scan base-package="com.code" >
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    package com.code.service.impl;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.code.dao.TestDAO;
    import com.code.entity.Test;
    import com.code.service.TestService;
    
    @Service
    public class TestServiceImpl implements TestService{
    	
    	@Resource
    	private TestDAO testDAO;
    	
    	@Override
    	@Transactional
    	public void sql() {
    		Test t1 =  new Test();
    			t1.setId(1L);
    			t1.setNum(800);
    		Test t2 =  new Test();
    			t2.setId(2L);
    			t2.setNum(1200);
    		
    		testDAO.update(t1);
    		int bug = 1/0;
    		testDAO.update(t2);
    		
    	}
    
    }

    已解决。都没人回复我  好失望。。。

  • 假象
    2015-09-08 16:08:23

    springMVC + mybits + mysql