一,Spring中基於xml文件配置事務管理
<?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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置賬戶的業務層-->
<bean id="accountService" class="com.lzw.service.impl.AccountServiceImpl">
<!--給accountDao注入數據-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置賬戶的持久層-->
<bean id="accountDao" class="com.lzw.dao.impl.AccountDaoImpl">
<!--給AccountDaoImpl中的jdbcTemplate注入數據-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置spring內置數據源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!--spring中基於xml的聲明式事務控制配置步驟
配置事務管理器
配置事務通知
導入事務的約束 tx名稱空間約束,同時也需要aop的
使用tx:advice標簽配置事務通知
屬性
id 給事務唯一通知
transaction-manager 給事務通知一個事務管理器用
配置aop通用切入點表達式
建立事務通知和切入點表達式對應關系
配置事務屬性
在事務的通知tx:advice標簽內部
-->
<!--配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事務通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配置事務屬性
isolation 用於指定隔離級別 默認是default 表示使用數據庫的隔離級別
propagation 指定事務的傳播行為 默認是required 表示一定有事務 增刪改的選擇 查詢方法可以選擇 supports
read-only 指定事務是否只讀 只有查詢方法才能設置為true 默認是false
timeout 指定事務的超時時間 默認是-1 表示永不超時 如果指定了數據 單位為秒
rollback-for 用於指定一個異常 當產生異常 事務回滾 產生其他異常 事務不回滾 沒有默認值 任何異常都回滾
no-rollback-for 指定一個異常 當產生異常時 事務不回滾 產生其他異常時 事務回滾 沒有默認值 表示任何異常都回滾
-->
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置aop-->
<aop:config>
<!--配置切入點表達式-->
<aop:pointcut id="pt1" expression="execution(* com.lzw.service.impl.*.*(..))"></aop:pointcut>
<!--建立切入點表達式和事務通知的對應關系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
二,Spring中基於純注解文件配置事務管理
package com.lzw.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* spring中的配置類 相當於bean.xml
*/
@Configuration //配置類
@ComponentScan("com.lzw") //創建srping容器時通知要掃描的包
@Import({JdbcConfig.class,TransactionManagerConfig.class}) //導入其他配置類
@PropertySource(value="jdbcConfig.properties") //引入外部資源
@EnableTransactionManagement //注解開啟spring事務支持
public class SpringConfiguration {}
package com.lzw.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
/**
* 連接數據庫相關的數據類
*/
public class JdbcConfig {
//注入基本數據類型和String類型數據 使用el表達式 注入數據
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 創建jdbctemplate對象
* @param dataSource
* @return
*/
//該注解只能寫在方法上,表明使用此方法創建一個對象,並放入spring容器
@Bean(name="jdbcTemplate")
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
/**
* 創建數據源
* @return
*/
@Bean(name="dataSource")
public DataSource createDataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
package com.lzw.config;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* 和事務相關的配置
*/
public class TransactionManagerConfig {
/**
* 創建數據管理器對象
* @param dataSource
* @return
*/
@Bean(name="transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
package com.lzw.service.impl;
import com.lzw.dao.IAccountDao;
import com.lzw.domian.Account;
import com.lzw.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* 業務層
*/
@Service("accountService") //創建accountService放入容器中
@Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只讀型事務的配置
public class AccountServiceImpl implements IAccountService {
@Autowired //自動按照成員變量類型注入
private IAccountDao accountDao;
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)//需要的是讀寫型事務配置
public void transfer(String sourceName, String targetName, Float money) {
//根據名稱查出轉出賬戶
Account source = accountDao.findAccountByName(sourceName);
//根據名稱查出轉入賬戶
Account target = accountDao.findAccountByName(targetName);
//轉出賬戶減錢
source.setMoney(source.getMoney() - money);
//轉入賬戶加前
target.setMoney(target.getMoney() + money);
//更新轉出賬戶
accountDao.updateAccount(source);
// int i = 1 / 0;
//更新轉入賬戶
accountDao.updateAccount(target);
}
}
package com.lzw.dao.impl;
import com.lzw.dao.IAccountDao;
import com.lzw.domian.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 賬戶持久層實現類
*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 根據id查詢用戶
* @param id
* @return
*/
public Account findAccountById(Integer id) {
List<Account> accountList = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),id);
return (accountList.isEmpty()? null : accountList.get(0));
}
/**
* 根據name查詢id
* @param accountname
* @return
*/
public Account findAccountByName(String accountname) {
List<Account> accountList = jdbcTemplate.query("select * from account where name = ?",
new BeanPropertyRowMapper<Account>(Account.class),accountname);
if(accountList.isEmpty()){
return null;
}
if(accountList.size() > 1){
throw new RuntimeException("結果集不唯一");
}
return accountList.get(0);
}
/**
* 更新用戶
* @param account
*/
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}
在實際開發我們更常用的是xml和注解的結合使用