Spring_day4

Springのトランザクション制御

XMLベースの宣言型トランザクション制御(構成方法)

  1. ステップ1:必要なjarパッケージをプロジェクトのlibディレクトリにコピーする
 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  1. ステップ2:Spring構成ファイルを作成して制約をインポートする
<?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">
</beans>
  1. 手順3:構成ファイルでビジネスレイヤーと永続化レイヤーのペアを構成する
<!-- 配置 service -->
<bean id="accountService" class="cn.myp666.service.impl.AccountServiceImpl">
	<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置 dao -->
<bean id="accountDao" class="cn.myp666.dao.impl.AccountDaoImpl">
<!-- 注入 dataSource -->
	<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql:///db1"></property>
	<property name="username" value="root"></property>
	<property name="password" value="1419"></property>
</bean>
トランザクション構成手順
  1. 手順1:トランザクションマネージャーを構成する
<!-- 配置一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource -->
	<property name="dataSource" ref="dataSource"></property>
</bean>
  1. 手順2:トランザクションマネージャーを参照するようにトランザクション通知を構成する
<!-- 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
</tx:advice>
  1. ステップ3:トランザクションの属性を構成する
<!--在 tx:advice 标签内部 配置事务的属性 -->
<tx:attributes>
<!-- 指定方法名称:是业务核心方法
	read-only:是否是只读事务。默认 false,不只读。
	isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
	propagation:指定事务的传播行为。
	timeout:指定超时时间。默认值为:-1。永不超时。
	rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
没有默认值,任何异常都回滚。
	no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回
滚。没有默认值,任何异常都回滚。
-->
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
  1. ステップ4:AOPポイントカット式を構成する
<!-- 配置 aop -->
<aop:config>
	<!-- 配置切入点表达式 -->
	<aop:pointcut expression="execution(* cn.myp666.service.impl.*.*(..))"
id="pt1"/>
</aop:config>
  1. ステップ5:ポイントカット式とトランザクション通知の間の対応を構成する
<!-- 在 aop:config 标签内部:建立事务的通知和切入点表达式的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>



注釈ベースの構成

  1. ステップ1:必要なjarパッケージをプロジェクトのlibディレクトリにコピーする
  2. ステップ2:制約をインポートしてスキャン済みパッケージを構成するためのSpring構成ファイルを作成する
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<!-- 配置 spring 创建容器时要扫描的包 -->
	<context:component-scan base-package="cn.myp666"></context:component-scan>
	<!-- 配置 JdbcTemplate-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.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>
		<property name="url"
value="jdbc:mysql://localhost:3306/db1"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1419"></property>
	</bean>
</beans>
注釈の構成手順
  1. 手順1:トランザクションマネージャーを構成し、データソースを挿入する
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>
  1. ステップ2:ビジネスレイヤーで@Transactionalアノテーションを使用する
@Service("accountService")
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class AccountServiceImpl implements IAccountService {

	@Autowired
	private IAccountDao accountDao;
	@Override
	public Account findAccountById(Integer id) {
		return accountDao.findAccountById(id);
	}
	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void transfer(String sourceName, String targeName, Float money) {
		//1.根据名称查询两个账户
		Account source = accountDao.findAccountByName(sourceName);
		Account target = accountDao.findAccountByName(targeName);
		//2.修改两个账户的金额
		source.setMoney(source.getMoney()-money);//转出账户减钱
		target.setMoney(target.getMoney()+money);//转入账户加钱
		//3.更新两个账户
		accountDao.updateAccount(source);
		//int i=1/0;
		accountDao.updateAccount(target);
		}
}
  • この注釈の属性は、xmlの属性と同じ意味を持っています。この注釈は、インターフェース、クラス、およびメソッドに表示できます。
    インターフェイスに表示され、インターフェイスのすべての実装クラスがトランザクションをサポートしていることを示します。
    クラスに表示され、クラス内のすべてのメソッドにトランザクションサポート
    があることを示しますメソッドに表示され、メソッドにトランザクションサポートがあることを示します。
    上記の3つの位置の優先順位:メソッド>クラス>インターフェース

  1. 3番目のステップ:構成ファイルでアノテーショントランザクションのSpringサポートを開く
<!-- 开启 spring 对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
元の記事94件を公開 称賛された0 2097を訪問

おすすめ

転載: blog.csdn.net/qq_16836791/article/details/105013728