spring管理ibatis事務

< sqlMapConfig >
    
< sqlMap  resource ="com/angi/ibatis/maps/User.xml"   />
</ sqlMapConfig >
以上配置省去了transactionManager的配置,就會使用external(外部)事務管理(ExternalTransaction),即等同以下配置:
複製代碼
< sqlMapConfig >
    
< transactionManager  type ="EXTERNAL" >
                
<!-- 這個數據源其實沒有什麼意義,仍是取上面的省略方式吧 -->
        
< dataSource  type ="DBCP" >
        
</ dataSource >
    
</ transactionManager >
    
< sqlMap  resource ="com/angi/ibatis/maps/User.xml"   />
</ sqlMapConfig >
複製代碼
一、TransactionProxyFactoryBean
複製代碼
<? 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:p ="http://www.springframework.org/schema/p"
    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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
    
< bean  id ="userServiceProxy"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
        
< property  name ="transactionManager" >
            
< ref  bean ="transactionManager"   />
        
</ property >
        
< property  name ="target" >
            
< ref  local ="userService"   />
        
</ property >
        
< property  name ="transactionAttributes" >
            
< props >
                
<!--  這裏的方法簽名能夠精確到方法, 先懶惰一下全配置上  -->
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
</ beans >
複製代碼
二、TransactionInterceptor
複製代碼
<? 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:p ="http://www.springframework.org/schema/p"
    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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
    
< bean
        
class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
        
< property  name ="beanNames" >
            
< list >
                
< value > userService </ value >
            
</ list >
        
</ property >
        
< property  name ="interceptorNames" >
            
< list >
                
< value > transactionInterceptor </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="transactionInterceptor"
        class
="org.springframework.transaction.interceptor.TransactionInterceptor" >
        
< property  name ="transactionManager"  ref ="transactionManager"   />
        
< property  name ="transactionAttributes" >
            
< props >
                
<!--  這裏的方法簽名能夠精確到方法, 先懶惰一下全配置上  -->
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
</ beans >
複製代碼
三、AOP和TX配置
複製代碼
<? 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:p ="http://www.springframework.org/schema/p"
    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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"
        destroy-method
="close" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
<!--  須要引入aop的命名空間  -->
    
< aop:config >
        
<!--  切入點指明瞭在全部方法產生事務攔截操做  -->
        
< aop:pointcut  id ="serviceMethods"
            expression
="execution(* com.angi.ibatis.service.*.*(..))"   />
        
<!--  定義了將採用何種攔截操做,這裏引用到 txAdvice  -->
        
< aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="serviceMethods"   />
    
</ aop:config >
    
<!--  須要引入tx的命名空間  -->
    
<!--  這是事務通知操做,使用的事務管理器引用自 transactionManager  -->
    
< tx:advice  id ="txAdvice"  transaction-manager ="transactionManager" >
        
< tx:attributes >
            
<!--  指定哪些方法須要加入事務,這裏懶惰一下所有加入,能夠使用通配符來只加入須要的方法  -->
            
< tx:method  name ="*"  propagation ="REQUIRED"   />
        
</ tx:attributes >
    
</ tx:advice >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
</ beans >
複製代碼
四、anotation
複製代碼
<? 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: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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  須要引入tx的命名空間  -->
    
< tx:annotation-driven  transaction-manager ="transactionManager"   />
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
</ beans >
複製代碼
Java代碼:
複製代碼
@Transactional
    
public   void  doTransaction() {
        User user 
=   new  User();
        user.setName(
" 11111 " );
        user.setSex(
1 );
        userDao.saveUser(user);
        User user1 
=   new  User();
        user1.setName(
" Angikkk " );
        user1.setSex(
1 );
        userDao.saveUser(user1);
複製代碼

    }  mysql

相關文章
相關標籤/搜索