欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring如何基于xml實現(xiàn)聲明式事務(wù)控制

 更新時間:2020年10月09日 09:39:21   作者:一路繁花似錦繡前程  
這篇文章主要介紹了Spring如何基于xml實現(xiàn)聲明式事務(wù)控制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>A02spring</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.6</version>
    </dependency>
    <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.8.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

二、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
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd">

  <bean id="accountService" class="com.wuxi.services.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
  </bean>

  <bean id="accountDao" class="com.wuxi.daos.impl.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url"
         value="jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
  </bean>

<!--
spring中基于xml的聲明式事務(wù)控制配置步驟
  1、配置事務(wù)管理器
  2、配置事務(wù)的通知
  3、配置aop中的通用切入點表達式
  4、建立事務(wù)通知和切入點表達式的對應(yīng)關(guān)系
  5、配置事務(wù)的屬性
-->
  <!--事務(wù)管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!--事務(wù)的通知-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--
    事務(wù)的屬性
      isolation:用于指定事務(wù)的隔離級別。默認值是DEFAULE,表示使用數(shù)據(jù)庫的默認隔離級別。
      propagation:用于指定事務(wù)的傳播行為。默認值是REQUIRED,表示一定會有事務(wù),增刪改的選擇。查詢方法可以選擇SUPPORTYS。
      read-only:用于指定事務(wù)是否只讀。只有查詢方法才能設(shè)置為true。默認值是false,表示讀寫。
      timeout:用于指定事務(wù)的超時時間,默認值是-1,表示永不超時,如果指定了數(shù)值,以秒為單位。
      rollback-for:用于指定一個異常,當產(chǎn)生該異常時,事務(wù)回滾,產(chǎn)生其他異常時,事務(wù)不回滾。沒有默認值。表示任何異常都回滾。
      no-rollback-for:用于指定一個異常,當產(chǎn)生該異常時,事務(wù)不回滾,產(chǎn)生其他異常時事務(wù)回滾。沒有默認值。表示任何異常都回滾。
    -->
    <tx:attributes>
      <tx:method name="*" propagation="REQUIRED" read-only="false"/>
      <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  <aop:config>
    <!--切入點表達式-->
    <aop:pointcut id="ptc" expression="execution(* com.wuxi.services.*.*(..))"/>
    <!--切入點表達式和事務(wù)通知的對應(yīng)關(guān)系-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="ptc"></aop:advisor>
  </aop:config>

</beans>

三、實體類

package com.wuxi.beans;

import lombok.Data;

import java.io.Serializable;

@Data
public class Account implements Serializable {
  private Integer id;
  private String name;
  private Float money;
}

四、dao

1、接口

package com.wuxi.daos;
import com.wuxi.beans.Account;
public interface AccountDao {
  Account findAccountById(Integer accountId);
  Account findAccountByName(String accountName);
  void updateAccount(Account account);
}

2、實現(xiàn)類

package com.wuxi.daos.impl;

import com.wuxi.beans.Account;
import com.wuxi.daos.AccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
  @Override
  public Account findAccountById(Integer accountId) {
    List<Account> accounts = getJdbcTemplate().query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), accountId);
    return accounts.isEmpty() ? null : accounts.get(0);
  }

  @Override
  public Account findAccountByName(String accountName) {
    List<Account> accounts = getJdbcTemplate().query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), accountName);
    if (accounts.isEmpty()) {
      return null;
    }
    if (accounts.size() > 1) {
      throw new RuntimeException("結(jié)果集不唯一");
    }
    return accounts.get(0);
  }

  @Override
  public void updateAccount(Account account) {
    getJdbcTemplate().update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
  }
}

五、service

1、接口

package com.wuxi.services;
import com.wuxi.beans.Account;
public interface AccountService {
  Account findAccounById(Integer accountId);
  void transfer(String sourceName, String targetName, Float money);
}

2、實現(xiàn)類

package com.wuxi.services.impl;

import com.wuxi.beans.Account;
import com.wuxi.daos.AccountDao;
import com.wuxi.services.AccountService;

public class AccountServiceImpl implements AccountService {
  private AccountDao accountDao;

  public void setAccountDao(AccountDao accountDao) {
    this.accountDao = accountDao;
  }

  @Override
  public Account findAccounById(Integer accountId) {
    return accountDao.findAccountById(accountId);
  }

  @Override
  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.wuxi.tests;

import com.wuxi.services.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class MySpringTest {

  @Autowired
  private AccountService as;

  @Test
  public void testTransfer() {
    as.transfer("aaa", "bbb", 100f);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • maven模塊化開發(fā)部署實現(xiàn)方案

    maven模塊化開發(fā)部署實現(xiàn)方案

    有些用戶有定制化需求,需要添加新的模塊功能,因此需要平臺主體功能迭代的同時,非主體功能和定制化功能插件化,本文給大家介紹maven模塊化開發(fā)部署實現(xiàn)方案,感興趣的朋友一起看看吧
    2024-01-01
  • SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟

    SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟

    這篇文章主要介紹了SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟,文中通過代碼示例和圖文講解的非常詳細,對大家實現(xiàn)OneDrive文件上傳有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • 詳解Java動態(tài)字節(jié)碼技術(shù)

    詳解Java動態(tài)字節(jié)碼技術(shù)

    Java字節(jié)碼增強指的是在Java字節(jié)碼生成之后,對其進行修改,增強其功能,可減少冗余代碼,提高性能等。本文將詳細介紹Java動態(tài)字節(jié)碼技術(shù)。
    2021-05-05
  • Java值傳遞和引用傳遞詳解

    Java值傳遞和引用傳遞詳解

    這篇文章主要為大家詳細介紹了Java值傳遞和引用傳遞,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐

    SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐

    本文主要介紹了SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐,主要基于MyBatis-Plus 自帶的分頁插件 PaginationInterceptor,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • windows 部署JAVA環(huán)境安裝iDea的詳細步驟

    windows 部署JAVA環(huán)境安裝iDea的詳細步驟

    這篇文章主要介紹了windows 部署JAVA環(huán)境安裝iDea的詳細步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • springboot?web項目中?Set-Cookie?失敗原因及解決辦法

    springboot?web項目中?Set-Cookie?失敗原因及解決辦法

    這篇文章主要介紹了springboot?web項目中?Set-Cookie?失敗原因及解決辦法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-10-10
  • Nacos配置中心設(shè)計原理分析

    Nacos配置中心設(shè)計原理分析

    今天分享一下Nacos配置變更的相關(guān)知識點,現(xiàn)在使用Java生態(tài)如果使用微服務(wù),如果部署在K8s上,那么可能會使用ConfigMap來存儲配置文件,如果沒有使用K8s,那么基本上都使用Nacos來做配置中心,所以有必要了解一下Nacos的配置的知識點,本文只是對其中的部分實現(xiàn)原理進行分析
    2023-10-10
  • Jenkins如何使用DockerFile自動部署Java項目

    Jenkins如何使用DockerFile自動部署Java項目

    這篇文章主要介紹了Jenkins如何使用DockerFile自動部署Java項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • 在SpringBoot中無縫整合Dubbo的實現(xiàn)過程

    在SpringBoot中無縫整合Dubbo的實現(xiàn)過程

    微服務(wù)架構(gòu)已經(jīng)成為現(xiàn)代應(yīng)用開發(fā)的熱門趨勢,而Dubbo作為一款強大的分布式服務(wù)框架,與Spring?Boot的結(jié)合是構(gòu)建高性能微服務(wù)應(yīng)用的理想選擇,本文將詳細介紹如何在SpringBoot中無縫整合Dubbo,需要的朋友可以參考下
    2024-01-01

最新評論