生活随笔
收集整理的這篇文章主要介紹了
转账为demo,spring事务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring 事務使用
1. 業務代碼
package com
.lovely
.dao
.impl
;import com
.lovely
.dao
.AccountDao
;
import org
.springframework
.jdbc
.core
.JdbcTemplate
;
public class AccountDaoImpl implements AccountDao {private JdbcTemplate jdbcTemplate
;public void setJdbcTemplate(JdbcTemplate jdbcTemplate
) {this.jdbcTemplate
= jdbcTemplate
;}public void out(String outMan
, Double money
) {String sql
= "update account set money = money - ? where name = ?";jdbcTemplate
.update(sql
, new Object[]{money
, outMan
});}public void in(String inMan
, Double money
) {String sql
= "update account set money = money + ? where name = ?";jdbcTemplate
.update(sql
, new Object[]{money
, inMan
});}
}
package com
.lovely
.service
.impl
;import com
.lovely
.dao
.AccountDao
;
import com
.lovely
.service
.AccountService
;
public class AccountServiceImpl implements AccountService {private AccountDao accountDao
;public void setAccountDao(AccountDao accountDao
) {this.accountDao
= accountDao
;}public void transfer(String outMan
, String inMan
, Double money
) {accountDao
.in(inMan
, money
);System
.out
.println(1/0);accountDao
.out(outMan
, money
);}
}
package com
.lovely
.controller
;import com
.lovely
.service
.AccountService
;
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
public class AccountController {public static void main(String
[] args
) {ApplicationContext app
= new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService
= app
.getBean(AccountService
.class);accountService
.transfer("jack", "rose", 1000.0);}
}
2. xml配置
<bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><tx:advice id="txAdvice" transaction-manager="tranManager"><tx:attributes><tx:method name="*"/></tx:attributes></tx:advice><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lovely.service.impl.*.*(..))"/></aop:config>
3. 注解配置
注解配置
<bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><tx:annotation-driven transaction-manager="tranManager"></tx:annotation-driven>
事務注解
package com
.lovely
.service
.impl
;import com
.lovely
.dao
.AccountDao
;
import com
.lovely
.service
.AccountService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
import org
.springframework
.transaction
.annotation
.Transactional
;@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao
;@Transactional(timeout
= 1)public void transfer(String outMan
, String inMan
, Double money
) {accountDao
.in(inMan
, money
);System
.out
.println(1/0);accountDao
.out(outMan
, money
);}
}
總結
以上是生活随笔為你收集整理的转账为demo,spring事务的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。