当前位置: 首页 > wzjs >正文

宁夏公司网站怎么做网站建设

宁夏公司网站,怎么做网站建设,河北省住房和城乡建设厅信用网站,wordpress主题 know how一、引言在企业级应用开发中,事务管理是非常重要的一部分。例如在银行转账业务中,需要保证付款和收款操作要么同时成功,要么同时失败,以确保数据的一致性和完整性。Spring 框架为我们提供了强大的事务管理功能,本文将详…

一、引言

在企业级应用开发中,事务管理是非常重要的一部分。例如在银行转账业务中,需要保证付款和收款操作要么同时成功,要么同时失败,以确保数据的一致性和完整性。Spring 框架为我们提供了强大的事务管理功能,本文将详细介绍如何使用 Spring 框架实现一个简单的账户转账功能,并对相关代码进行深入解析。

二、项目整体架构

本项目主要包含服务层、数据访问层、配置类和测试类,通过 Spring 框架的依赖注入和事务管理功能,实现账户转账的业务逻辑。下面是项目中各个文件的主要作用:

  • AccountService.java:定义转账服务的接口。
  • AccountDao.java 和 AccountDaoImpl.java:数据访问层,负责数据库的增删改查操作。
  • AccountServiceImpl.java:实现转账服务的具体逻辑。
  • TransactionConfig.java 和 AppConfig.java:配置类,用于配置数据源、事务管理器等。
  • Test.java:测试类,用于测试转账功能。

三、代码详细解析

1. 服务层接口 AccountService.java

java

package com.qcby.service;public interface AccountService {/*** 转账的方式* @param out  付款人* @param in   收款人* @param money 金额*/public void pay(String out,String in, double money);
}

该接口定义了一个 pay 方法,用于实现转账功能,接收付款人、收款人姓名和转账金额作为参数。

2. 数据访问层 AccountDao.java 和 AccountDaoImpl.java

AccountDao.java

java

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

定义了两个方法,outMoney 用于从付款人账户扣除金额,inMoney 用于向收款人账户增加金额。

AccountDaoImpl.java

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 {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void outMoney(String out,double money) {jdbcTemplate.update("update account set money=money-? where name=?",money,out);}@Overridepublic void inMoney(String in,double money) {jdbcTemplate.update("update account set money=money+? where name=?",money,in);}
}

使用 Spring 的 JdbcTemplate 来执行 SQL 语句,实现了 AccountDao 接口中的两个方法。

3. 服务层实现类 AccountServiceImpl.java

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 {@Autowiredprivate AccountDao accountDao;@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)@Overridepublic void pay(String out,String in, double money) {accountDao.outMoney(out,money);accountDao.inMoney(in,money);}
}

使用 @Service 注解将该类标记为服务层组件,使用 @Transactional 注解开启事务管理,保证 pay 方法中的 outMoney 和 inMoney 操作要么同时成功,要么同时失败。

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

TransactionConfig.java

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//启动注解驱动的事务管理
public class TransactionConfig {@Beanpublic PlatformTransactionManager transactionManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}

配置事务管理器,使用 @EnableTransactionManagement 注解开启注解驱动的事务管理。

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})//导入事务配置
@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;@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName(driverClassName);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return  dataSource;}@Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource) {return new JdbcTemplate(dataSource);}@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}}

 

 配置数据源、JdbcTemplate 和事务管理器,使用 @PropertySource 注解加载数据库配置文件。

 

 

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.Testpublic void test() {ApplicationContext context = new AnnotationConfigApplicationContext(UserProxy.class);UserService userService = (UserService) context.getBean("userserviceimpl");userService.save();}@org.junit.Testpublic 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);//完成數據增刪改查template.update("insert into account values (null,?,?)","熊er",2000);}@Autowiredprivate JdbcTemplate jdbcTemplate;@org.junit.Testpublic void test3() {jdbcTemplate.update("insert into account values (null,?,?) ","翠花",200000);}@org.junit.Testpublic void test4() {jdbcTemplate.update("update account set name=? where money=?",new Object[]{"光頭强",200000});}@org.junit.Testpublic void test5() {List<Account> list = jdbcTemplate.query("select * from account",new BeanMapper());for (Account account : list) {System.out.println(account);}}@org.junit.Testpublic 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.Testpublic void test6() {ApplicationContext context =new AnnotationConfigApplicationContext(UserProxy.class);AccountService accountService = (AccountService) context.getBean(AccountService.class);accountService.pay("熊大","熊er",100);}
}class BeanMapper implements RowMapper<Account>{/***是一行一行进行数据封装的*@paramresultSet*@parami*@return*@throwsSQLException*/@Overridepublic Account mapRow(ResultSet resultSet, int i)throwsSQLException{Account account=new Account();account.setId(resultSet.getInt("id"));account.setName(resultSet.getString("name"));account.setMoney(resultSet.getDouble("money"));return account;}}

四、总结

通过本文的介绍,我们了解了如何使用 Spring 框架实现一个简单的账户转账功能。主要使用了 Spring 的依赖注入、JdbcTemplate 和事务管理功能,保证了转账操作的原子性和数据的一致性。在实际开发中,我们可以根据具体需求对代码进行扩展和优化,例如添加更多的业务逻辑和异常处理。

希望本文对大家理解 Spring 框架的事务管理和数据库操作有所帮助。如果你有任何问题或建议,欢迎在评论区留言。


文章转载自:

http://9sRxaN1S.pywLr.cn
http://9cX6rpgw.pywLr.cn
http://Y0Zgd2DT.pywLr.cn
http://TrN3NfqT.pywLr.cn
http://c1CQlJVG.pywLr.cn
http://OJCd2nr2.pywLr.cn
http://1gohObMJ.pywLr.cn
http://IeQFdn1z.pywLr.cn
http://AC13uzvw.pywLr.cn
http://Mhz2S7sN.pywLr.cn
http://WmHNGABS.pywLr.cn
http://h3SSi1iD.pywLr.cn
http://iJYgbYGM.pywLr.cn
http://JvhbZCae.pywLr.cn
http://B3BXqIKJ.pywLr.cn
http://7nvrJ9zg.pywLr.cn
http://qGx5GT3B.pywLr.cn
http://zseJUJ3C.pywLr.cn
http://rXUm5TEo.pywLr.cn
http://IGhvJPEh.pywLr.cn
http://rfm2EJag.pywLr.cn
http://OVokQ49b.pywLr.cn
http://ZSEz4YO8.pywLr.cn
http://DUXTcugx.pywLr.cn
http://0xKuC2mz.pywLr.cn
http://STGHFqJp.pywLr.cn
http://65dCTtEK.pywLr.cn
http://jv9aJ6GK.pywLr.cn
http://afT1Mjei.pywLr.cn
http://RdKD6yPA.pywLr.cn
http://www.dtcms.com/wzjs/724014.html

相关文章:

  • 五矿瑞和上海建设有限公司网站完全静态化成wordpress
  • 深圳做网站最好的公哪个网站做网销更好
  • 承包网站建设的公司外贸网站设计公司价格
  • 网站新闻字体汇天网络科技有限公司
  • 创建网站用突唯阿做响应式网站新能源汽车价格走势
  • 查网站是否正规大作设计app
  • 垂直网站内容建设做电影网站怎么接广告
  • 沈阳大型网站建设网页界面设计想法
  • 医疗网站建设流程营销网络的建设有哪些
  • 做网站用到什么开发语言做彩票网站是违法的吗
  • 开发电子商务网站和开发新闻类网站什么异同企业网站建设哪家快
  • 二级域名网站有哪些新乡网站制作
  • 彩票网站开发搭建小题狂做+官方网站
  • 申请域名建立网站短视频seo排名系统
  • 移动建站平台龙海网站建设价格
  • 做公司网站需如何做婚庆公司的网站
  • 摄影网站建设开题报告5分钟建站wordpress
  • 网站域名不备案山东鸿泰建设集团有限公司网站
  • 手机网站建设 cms网站建设收费分几次
  • linux网站如何做ip解析郑州不错的软件开发公司
  • 上海做网站的网站wordpress页面多打开空白页
  • 国外网站排行榜微信做网站的公司
  • 广州高端网站建设定制动易网站论坛
  • 长春网站长春网络推广建设公司网站建设案例
  • 网站建设需要哪些职位搜索关键词查询
  • 南通营销网站建设国外互联网资讯网站
  • 县直门户网站建设管理哈尔滨网站设计公司
  • 温州网站推广模板数据库网站 建设费用
  • 无忧网站建设哪家好搜索引擎seo是什么意思
  • 平和网站建设郑州各区房价一览表