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

Spring 框架實現(xiàn)賬戶轉(zhuǎn)賬功能(推薦)

 更新時間:2025年07月09日 09:58:27   作者:小白的代碼日記  
通過本文的介紹,我們了解了如何使用Spring框架實現(xiàn)一個簡單的賬戶轉(zhuǎn)賬功能,主要使用了 Spring 的依賴注入、和事務(wù)管理功能,保證了轉(zhuǎn)賬操作的原子性和數(shù)據(jù)的一致性,感興趣的朋友跟隨小編一起看看吧

一、引言

在企業(yè)級應(yīng)用開發(fā)中,事務(wù)管理是非常重要的一部分。例如在銀行轉(zhuǎn)賬業(yè)務(wù)中,需要保證付款和收款操作要么同時成功,要么同時失敗,以確保數(shù)據(jù)的一致性和完整性。Spring 框架為我們提供了強(qiáng)大的事務(wù)管理功能,本文將詳細(xì)介紹如何使用 Spring 框架實現(xiàn)一個簡單的賬戶轉(zhuǎn)賬功能,并對相關(guān)代碼進(jìn)行深入解析。

二、項目整體架構(gòu)

本項目主要包含服務(wù)層、數(shù)據(jù)訪問層、配置類和測試類,通過 Spring 框架的依賴注入和事務(wù)管理功能,實現(xiàn)賬戶轉(zhuǎn)賬的業(yè)務(wù)邏輯。下面是項目中各個文件的主要作用:

  • AccountService.java:定義轉(zhuǎn)賬服務(wù)的接口。
  • AccountDao.java 和 AccountDaoImpl.java:數(shù)據(jù)訪問層,負(fù)責(zé)數(shù)據(jù)庫的增刪改查操作。
  • AccountServiceImpl.java:實現(xiàn)轉(zhuǎn)賬服務(wù)的具體邏輯。
  • TransactionConfig.java 和 AppConfig.java:配置類,用于配置數(shù)據(jù)源、事務(wù)管理器等。
  • Test.java:測試類,用于測試轉(zhuǎn)賬功能。

三、代碼詳細(xì)解析

1. 服務(wù)層接口 AccountService.java

package com.qcby.service;
public interface AccountService {
    /**
     * 轉(zhuǎn)賬的方式
     * @param out  付款人
     * @param in   收款人
     * @param money 金額
     */
    public void pay(String out,String in, double money);
}

該接口定義了一個 pay 方法,用于實現(xiàn)轉(zhuǎn)賬功能,接收付款人、收款人姓名和轉(zhuǎn)賬金額作為參數(shù)。

2. 數(shù)據(jù)訪問層AccountDao.java和 AccountDaoImpl.java

AccountDao.java

package com.qcby.dao;
public interface AccountDao {
    void outMoney(String out,double money);
    void inMoney(String in,double money);
}

定義了兩個方法,outMoney 用于從付款人賬戶扣除金額,inMoney 用于向收款人賬戶增加金額。

AccountDaoImpl.java

package com.qcby.dao.Impl;
import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void outMoney(String out,double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,out);
    }
    @Override
    public void inMoney(String in,double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,in);
    }
}

使用 Spring 的 JdbcTemplate 來執(zhí)行 SQL 語句,實現(xiàn)了 AccountDao 接口中的兩個方法。

3. 服務(wù)層實現(xiàn)類 AccountServiceImpl.java

package com.qcby.service.impl;
import com.qcby.dao.AccountDao;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Transactional(
            isolation = Isolation.DEFAULT,
            propagation = Propagation.REQUIRED
    )
    @Override
    public void pay(String out,String in, double money) {
        accountDao.outMoney(out,money);
        accountDao.inMoney(in,money);
    }
}

使用 @Service 注解將該類標(biāo)記為服務(wù)層組件,使用 @Transactional 注解開啟事務(wù)管理,保證 pay 方法中的 outMoney 和 inMoney 操作要么同時成功,要么同時失敗。

4. 配置類 TransactionConfig.java 和 AppConfig.java

TransactionConfig.java

package com.qcby.Utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement//啟動注解驅(qū)動的事務(wù)管理
public class TransactionConfig {
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

配置事務(wù)管理器,使用 @EnableTransactionManagement 注解開啟注解驅(qū)動的事務(wù)管理。

AppConfig.java

package com.qcby.Utils;
import com.qcby.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
@Import({TransactionConfig.class})//導(dǎo)入事務(wù)配置
@EnableAspectJAutoProxy(proxyTargetClass=true)
@EnableTransactionManagement
public class AppConfig {
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return  dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

 配置數(shù)據(jù)源、JdbcTemplate 和事務(wù)管理器,使用 @PropertySource 注解加載數(shù)據(jù)庫配置文件。

5. 測試類 Test.java

import com.qcby.Utils.AppConfig;
import com.qcby.Utils.UserProxy;
import com.qcby.entity.Account;
import com.qcby.service.AccountService;
import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;
import org.aspectj.lang.annotation.Around;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class, UserProxy.class})
public class Test {
    @org.junit.Test
    public void test() {
        ApplicationContext context = new AnnotationConfigApplicationContext(UserProxy.class);
        UserService userService = (UserService) context.getBean("userserviceimpl");
        userService.save();
    }
    @org.junit.Test
    public void test2() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/spring_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        JdbcTemplate template = new JdbcTemplate(dataSource);
        //完成數(shù)據(jù)增刪改查
        template.update("insert into account values (null,?,?)","熊er",2000);
    }
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @org.junit.Test
    public void test3() {
        jdbcTemplate.update("insert into account values (null,?,?) ","翠花",200000);
    }
    @org.junit.Test
    public void test4() {
        jdbcTemplate.update("update account set name=? where money=?",new Object[]{"光頭強(qiáng)",200000});
    }
    @org.junit.Test
    public void test5() {
        List<Account> list = jdbcTemplate.query("select * from account",new BeanMapper());
        for (Account account : list) {
            System.out.println(account);
        }
    }
    @org.junit.Test
    public void Pay() {
        String out = "熊大";
        String in="熊er";
        double money=500;
        ApplicationContext context =new ClassPathXmlApplicationContext("Spring.xml");
        AccountService accountService = (AccountService) context.getBean("accountService");
        accountService.pay(out,in,money);
    }
    @org.junit.Test
    public void test6() {
        ApplicationContext context =new AnnotationConfigApplicationContext(UserProxy.class);
        AccountService accountService = (AccountService) context.getBean(AccountService.class);
        accountService.pay("熊大","熊er",100);
    }
}
class BeanMapper implements RowMapper<Account>{
    /**
     *是一行一行進(jìn)行數(shù)據(jù)封裝的
     *@paramresultSet
     *@parami
     *@return
     *@throwsSQLException
     */
    @Override
    public Account mapRow(ResultSet resultSet, int i)throws
    SQLException{
        Account account=new Account();
        account.setId(resultSet.getInt("id"));
        account.setName(resultSet.getString("name"));
        account.setMoney(resultSet.getDouble("money"));
        return account;
    }
}

四、總結(jié)

通過本文的介紹,我們了解了如何使用 Spring 框架實現(xiàn)一個簡單的賬戶轉(zhuǎn)賬功能。主要使用了 Spring 的依賴注入、JdbcTemplate 和事務(wù)管理功能,保證了轉(zhuǎn)賬操作的原子性和數(shù)據(jù)的一致性。在實際開發(fā)中,我們可以根據(jù)具體需求對代碼進(jìn)行擴(kuò)展和優(yōu)化,例如添加更多的業(yè)務(wù)邏輯和異常處理。

希望本文對大家理解 Spring 框架的事務(wù)管理和數(shù)據(jù)庫操作有所幫助。如果你有任何問題或建議,歡迎在評論區(qū)留言。

到此這篇關(guān)于Spring 框架實現(xiàn)賬戶轉(zhuǎn)賬功能全解析的文章就介紹到這了,更多相關(guān)Spring賬戶轉(zhuǎn)賬內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 從互聯(lián)網(wǎng)上爬郵箱代碼示例

    Java 從互聯(lián)網(wǎng)上爬郵箱代碼示例

    這篇文章介紹了Java 從互聯(lián)網(wǎng)上爬郵箱的有關(guān)內(nèi)容,主要是一個代碼示例,小編覺得挺不錯的,這里給大家分享下,需要的朋友可以了解。
    2017-10-10
  • 使用Spring Cloud Gateway實現(xiàn)動態(tài)路由的核心原理

    使用Spring Cloud Gateway實現(xiàn)動態(tài)路由的核心原理

    Spring Cloud Gateway 實現(xiàn)動態(tài)路由的核心是將路由規(guī)則從靜態(tài)配置遷移到外部數(shù)據(jù)源,并通過事件機(jī)制實時刷新路由緩存,本文介紹使用Spring Cloud Gateway實現(xiàn)動態(tài)路由的核心原理,感興趣的朋友跟隨小編一起看看吧
    2025-10-10
  • 基于Spring Boot 的小區(qū)人臉識別與出入記錄管理系統(tǒng)功能

    基于Spring Boot 的小區(qū)人臉識別與出入記錄管理系統(tǒng)功能

    文章介紹基于SpringBoot框架與百度AI人臉識別API的小區(qū)出入管理系統(tǒng),實現(xiàn)自動識別、記錄及查詢功能,涵蓋技術(shù)選型、數(shù)據(jù)模型設(shè)計、接口開發(fā)與系統(tǒng)優(yōu)化方案,為智慧社區(qū)提供高效安全管理工具,感興趣的朋友跟隨小編一起看看吧
    2025-08-08
  • SpringCloud通過MDC實現(xiàn)分布式鏈路追蹤

    SpringCloud通過MDC實現(xiàn)分布式鏈路追蹤

    在DDD領(lǐng)域驅(qū)動設(shè)計中,我們使用SpringCloud來去實現(xiàn),但排查錯誤的時候,通常會想到Skywalking,但是引入一個新的服務(wù),增加了系統(tǒng)消耗和管理學(xué)習(xí)成本,對于大型項目比較適合,但是小的項目顯得太過臃腫了,所以本文介紹了SpringCloud通過MDC實現(xiàn)分布式鏈路追蹤
    2024-11-11
  • java使用swt顯示圖片示例分享

    java使用swt顯示圖片示例分享

    這篇文章主要介紹了java使用swt顯示圖片示例,修改后就可變?yōu)閳D片瀏覽器,需要的朋友可以參考下
    2014-02-02
  • Java設(shè)計模式之裝飾模式詳解

    Java設(shè)計模式之裝飾模式詳解

    這篇文章主要介紹了Java設(shè)計模式中的裝飾者模式,裝飾者模式即Decorator?Pattern,裝飾模式是在不必改變原類文件和使用繼承的情況下,動態(tài)地擴(kuò)展一個對象的功能,裝飾模式又名包裝模式。裝飾器模式以對客戶端透明的方式拓展對象的功能,是繼承關(guān)系的一種替代方案
    2022-07-07
  • Java中&&與?表達(dá)式結(jié)合時出現(xiàn)的坑

    Java中&&與?表達(dá)式結(jié)合時出現(xiàn)的坑

    這篇文章主要給大家介紹了關(guān)于Java中&&與?表達(dá)式結(jié)合時出現(xiàn)的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Jenkins分布式集群配置方式

    Jenkins分布式集群配置方式

    這篇文章主要介紹了Jenkins分布式集群配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-07-07
  • 關(guān)于logback日志級別動態(tài)切換的四種方式

    關(guān)于logback日志級別動態(tài)切換的四種方式

    這篇文章主要介紹了關(guān)于logback日志級別動態(tài)切換的四種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • nacos配置在代碼中引用的方法講解

    nacos配置在代碼中引用的方法講解

    這篇文章主要介紹了nacos配置在代碼中如何引用,如果主配置中配置的內(nèi)容和拓展配置的內(nèi)容重復(fù)則按主配置的配置 ,如果拓展配置中的內(nèi)容和另一個拓展配置中的內(nèi)容重復(fù),則按下標(biāo)大的配置作為最終的配置,對nacos配置代碼引用相關(guān)知識感興趣朋友一起看看吧
    2022-12-12

最新評論

芦山县| 沂源县| 调兵山市| 天津市| 郁南县| 本溪| 贡觉县| 潮州市| 沧州市| 建水县| 射阳县| 三都| 平度市| 丁青县| 中宁县| 广汉市| 禄劝| 奉贤区| 淮南市| 康保县| 绥德县| 犍为县| 昆明市| 德江县| 万山特区| 贞丰县| 乌鲁木齐县| 朔州市| 岳普湖县| 芜湖县| 米林县| 乐都县| 都昌县| 宁化县| 乌恰县| 来安县| 阿坝县| 江口县| 遂川县| 平利县| 阿巴嘎旗|