-
<!-- 配置業(yè)務(wù)層類 --> <bean id="accountService" class="cn.muke.spring.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> <!-- 注入事務(wù)管理的模版 --> 哪個(gè)類需要事務(wù)管理,就在哪個(gè)類中注入事務(wù)管理模版 <property name="transactionTemplate" ref="transactionTemplate"/> </bean> <!-- 配置DAO類 --> <bean id="accountDao" class="cn.muke.spring.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事務(wù)管理模版,spring為了簡(jiǎn)化事務(wù)管理的代碼而提供的類 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean>查看全部
-
事務(wù)管理模版是真正來(lái)進(jìn)行事務(wù)管理的類,數(shù)據(jù)庫(kù)連接池可以獲得到具體的連接對(duì)象查看全部
-
/** * 轉(zhuǎn)賬案例的測(cè)試類 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringDemo1 { //測(cè)試業(yè)務(wù)層類 @Resource(name="accountService") private AccountService accountService; @Test public void demo1() { accountService.transfer("aaa","bbb",200.0); } }查看全部
-
@Override public void transfer(String out, String in, Double money) { accountDao.outMoney(out, money); int i = 1 / 0; accountDao.inMoney(in, money); } 沒(méi)有事務(wù)事務(wù),會(huì)導(dǎo)致 accountDao.outMoney(out, money);被執(zhí)行 錢(qián)扣出去了 但是 int i = 1 / 0;會(huì)報(bào)異常,也就是 accountDao.inMoney(in, money);不會(huì)執(zhí)行,等于錢(qián)扣出去了但是收錢(qián)的錢(qián)沒(méi)有增加,這就是沒(méi)有使用事務(wù)的弊端 ------------------------------------ 事務(wù)保證要么一起全部成功,要么一起失敗查看全部
-
<!-- 引入外部屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0數(shù)據(jù)庫(kù)連接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>查看全部
-
/** * 轉(zhuǎn)賬案例的業(yè)務(wù)層實(shí)現(xiàn)類 */ public class AccountServiceImpl implements AccountService { //注入轉(zhuǎn)賬的DAO類,使service能夠調(diào)用DAO的方法 private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfer(String out, String in, Double money) { accountDao.outMoney(out, money); accountDao.inMoney(in, money); } }查看全部
-
/** * 轉(zhuǎn)賬案例的業(yè)務(wù)層接口 */ public interface AccountService { /** * @param out 轉(zhuǎn)出帳號(hào) * @param in 轉(zhuǎn)入帳號(hào) * @param money 轉(zhuǎn)賬金額 */ public void transfer(String out, String in, Double money); }查看全部
-
/** * 轉(zhuǎn)賬案例DAO層接口 */ public interface AccountDao { /** * @param out 轉(zhuǎn)出帳號(hào) * @param money 轉(zhuǎn)賬金額 */ public void outMoney(String out, Double money); /** * @param in 轉(zhuǎn)入帳號(hào) * @param money 轉(zhuǎn)入金額 */ public void inMoney(String in, Double money); } <!-- 配置DAO類 --> <bean id="accountDao" class="cn.muke.spring.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>查看全部
-
/** * 轉(zhuǎn)賬案例DAO層實(shí)現(xiàn)類 */ public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { /** * @param out 轉(zhuǎn)出帳號(hào) * @param money 轉(zhuǎn)賬金額 */ @Override public void outMoney(String out, Double money) { String sql = "UPDATE account SET money = money - ? WHERE name = ?"; this.getJdbcTemplate().update(sql, money, out); } /** * @param in 轉(zhuǎn)入帳號(hào) * @param money 轉(zhuǎn)入金額 */ @Override public void inMoney(String in, Double money) { String sql = "UPDATE account SET moeny = money + ? WHERE name = ?"; this.getJdbcTemplate().update(sql, money, in); } }查看全部
-
/** * 轉(zhuǎn)賬案例的業(yè)務(wù)層實(shí)現(xiàn)類 */ public class AccountServiceImpl implements AccountService { //注入轉(zhuǎn)賬的DAO類,使service能夠調(diào)用DAO的方法 private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfer(String out, String in, Double money) { accountDao.outMoney(out, money); accountDao.inMoney(in, money); } }查看全部
-
//注入轉(zhuǎn)賬的DAO類,使service能夠調(diào)用DAO的方法 private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } <!-- 配置業(yè)務(wù)層類 --> <bean id="accountService" class="cn.muke.spring.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean>查看全部
-
<?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" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> 最全spring約束配置查看全部
-
Spring支持兩種方式事務(wù)管理查看全部
-
TransactionStatus接口用來(lái)記錄事務(wù)的狀態(tài) 該接口定義了一組方法,用來(lái)獲取或判斷事務(wù)的相應(yīng)狀態(tài)信息.查看全部
-
事務(wù)傳播行為 七種 三類 重點(diǎn)記標(biāo)紅的 第一類:前三個(gè) 重點(diǎn)第一個(gè) 第二類:中間三個(gè) 重點(diǎn)第一個(gè) 主要在做aaa()和bbb()沒(méi)有在同一事務(wù)里邊的操作 第三類:最后一個(gè) 最后一個(gè) - 嵌套事務(wù) - ?執(zhí)行完aaa()的時(shí)候,使用事務(wù)設(shè)置了一個(gè)保存點(diǎn),再執(zhí)行bbb(),如果bbb()沒(méi)有異常,就一起提交了 如果有異常,會(huì)根據(jù)自己的設(shè)定(可以設(shè)置為回滾到保存點(diǎn)的位置,也可以回滾到初始狀態(tài)) ----------------- 重點(diǎn)記標(biāo)紅的 分別表示 在同一事務(wù)中 不在同一事務(wù)中 事務(wù)嵌套 - aaa事務(wù)完成之后設(shè)置一個(gè)保存點(diǎn),如果bbb發(fā)生異常,設(shè)置回滾到保存點(diǎn)的位置或者回滾到最初始化查看全部
舉報(bào)
0/150
提交
取消