最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、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中的通用切入點(diǎn)表達(dá)式
  4、建立事務(wù)通知和切入點(diǎn)表達(dá)式的對(duì)應(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ù)的隔離級(jí)別。默認(rèn)值是DEFAULE,表示使用數(shù)據(jù)庫(kù)的默認(rèn)隔離級(jí)別。
      propagation:用于指定事務(wù)的傳播行為。默認(rèn)值是REQUIRED,表示一定會(huì)有事務(wù),增刪改的選擇。查詢方法可以選擇SUPPORTYS。
      read-only:用于指定事務(wù)是否只讀。只有查詢方法才能設(shè)置為true。默認(rèn)值是false,表示讀寫(xiě)。
      timeout:用于指定事務(wù)的超時(shí)時(shí)間,默認(rèn)值是-1,表示永不超時(shí),如果指定了數(shù)值,以秒為單位。
      rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)回滾,產(chǎn)生其他異常時(shí),事務(wù)不回滾。沒(méi)有默認(rèn)值。表示任何異常都回滾。
      no-rollback-for:用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)不回滾,產(chǎn)生其他異常時(shí)事務(wù)回滾。沒(méi)有默認(rèn)值。表示任何異常都回滾。
    -->
    <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>
    <!--切入點(diǎn)表達(dá)式-->
    <aop:pointcut id="ptc" expression="execution(* com.wuxi.services.*.*(..))"/>
    <!--切入點(diǎn)表達(dá)式和事務(wù)通知的對(duì)應(yīng)關(guān)系-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="ptc"></aop:advisor>
  </aop:config>

</beans>

三、實(shí)體類

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、實(shí)現(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、實(shí)現(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);
  }
}

六、測(cè)試

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);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java數(shù)組集合的深度復(fù)制代碼實(shí)例

    Java數(shù)組集合的深度復(fù)制代碼實(shí)例

    這篇文章主要介紹了Java數(shù)組集合的深度復(fù)制代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java中使用DOM4J生成xml文件并解析xml文件的操作

    Java中使用DOM4J生成xml文件并解析xml文件的操作

    這篇文章主要介紹了Java中使用DOM4J來(lái)生成xml文件和解析xml文件的操作,今天通過(guò)代碼給大家展示了解析xml文件和生成xml文件的方法,需要的朋友可以參考下
    2021-09-09
  • Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析

    Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析

    為了簡(jiǎn)化MyBatis的使用,MyBatis提供了接口方式自動(dòng)化生成調(diào)用過(guò)程,可以大大簡(jiǎn)化MyBatis的開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • SpringBoot+Druid開(kāi)啟監(jiān)控頁(yè)面的實(shí)現(xiàn)示例

    SpringBoot+Druid開(kāi)啟監(jiān)控頁(yè)面的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot+Druid開(kāi)啟監(jiān)控頁(yè)面的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • java反射獲取方法參數(shù)名的幾種方式總結(jié)

    java反射獲取方法參數(shù)名的幾種方式總結(jié)

    這篇文章主要介紹了如何通過(guò)添加編譯參數(shù)或使用Spring的工具類來(lái)獲取方法參數(shù)名,還總結(jié)了不同版本的JDK和Spring項(xiàng)目中參數(shù)名獲取的優(yōu)缺點(diǎn),并提供了應(yīng)用場(chǎng)景舉例,需要的朋友可以參考下
    2025-02-02
  • SpringCloud Feign遠(yuǎn)程調(diào)用與自定義配置詳解

    SpringCloud Feign遠(yuǎn)程調(diào)用與自定義配置詳解

    Feign是Netflix公司開(kāi)發(fā)的一個(gè)聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開(kāi)發(fā)非?;A(chǔ)的組件,在使用的過(guò)程中我們也發(fā)現(xiàn)它們一般都是同時(shí)出現(xiàn)的,而且配置也都非常相似
    2022-11-11
  • 簡(jiǎn)單理解java泛型的本質(zhì)(非類型擦除)

    簡(jiǎn)單理解java泛型的本質(zhì)(非類型擦除)

    泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計(jì)模式中有非常廣泛的應(yīng)用。泛型是參數(shù)化類型的應(yīng)用,操作的數(shù)據(jù)類型不限定于特定類型,可以根據(jù)實(shí)際需要設(shè)置不同的數(shù)據(jù)類型,以實(shí)現(xiàn)代碼復(fù)用。下面小編來(lái)簡(jiǎn)單講一講泛型
    2019-05-05
  • SpringBoot數(shù)據(jù)庫(kù)查詢超時(shí)配置詳解

    SpringBoot數(shù)據(jù)庫(kù)查詢超時(shí)配置詳解

    這篇文章主要介紹了SpringBoot數(shù)據(jù)庫(kù)查詢超時(shí)配置,超時(shí)配置可以避免長(zhǎng)時(shí)間占用數(shù)據(jù)庫(kù)連接,提高系統(tǒng)的響應(yīng)速度和吞吐量,還可以快速的反饋可以提升用戶體驗(yàn),避免用戶因長(zhǎng)時(shí)間等待而感到挫敗,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-11-11
  • 詳解Java單元測(cè)試之JUnit篇

    詳解Java單元測(cè)試之JUnit篇

    這篇文章主要介紹了詳解Java單元測(cè)試之JUnit篇,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 利用Spring Session和redis對(duì)Session進(jìn)行共享詳解

    利用Spring Session和redis對(duì)Session進(jìn)行共享詳解

    這篇文章主要給大家介紹了關(guān)于利用Spring、Session和redis對(duì)Session進(jìn)行共享的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09

最新評(píng)論

廉江市| 河曲县| 沙坪坝区| 扶余县| 平罗县| 建昌县| 漳州市| 高碑店市| 永靖县| 班戈县| 潜山县| 池州市| 科尔| 元谋县| 红原县| 中西区| 内黄县| 武乡县| 南汇区| 蕲春县| 广河县| 西盟| 和龙市| 宜州市| 太保市| 乌拉特前旗| 历史| 垣曲县| 明溪县| 平阴县| 江山市| 英山县| 莲花县| 尉氏县| 弋阳县| 沈阳市| 扬州市| 新平| 康马县| 吐鲁番市| 南宫市|