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

Spring模拟转账开发

完成转账代码的编写
service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public void pay(String out, String in, Double money) {accountDao.outMoney(out,money);accountDao.inMoney(in,money);}
}
dao
public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void outMoney(String out, double money) {jdbcTemplate.update("update account set money = money - ? where name = ?",money,out);}public void inMoney(String in, double money) {jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);}
}
配置文件
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/><!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/>
</bean>
<!--配置jdbc模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/>
</bean>
<!--配置 service-->
<bean id="accountService" class="com.qcby.demo2.AccountServiceImpl"><property name="accountDao" ref="accountDao"/>
</bean>
<!--配置 dao-->
<bean id="accountDao" class="com.qcby.demo2.AccountDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {@Autowiredprivate AccountService accountService;@Testpublic void run1(){accountService.pay("熊大","熊二",10.0);}
}
Dao编写的方式(第二种方式)
service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public void pay(String out, String in, Double money) {accountDao.outMoney(out,money);accountDao.inMoney(in,money);}
}
dao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{//付款public void outMoney(String out, double money) {this.getJdbcTemplate().update("update account set money = money-? where name = ?",money,out);}public void inMoney(String in, double money) {this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,in);}
}
配置文件编写
<!--第二种写法:使用提供标签的方式-->
<context:property-placeholder location="classpath:jd.properties"/><!--加载属性的文件(使用开源连接池)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/>
</bean>
<!--配置 service-->
<bean id="accountService" class="com.qcby.demo2.AccountServiceImpl"><property name="accountDao" ref="accountDao"/>
</bean>
<!--配置 dao-->
<bean id="accountDao" class="com.qcby.demo2.AccountDaoImpl"><property name="dataSource" ref="dataSource" />
</bean>
测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo1 {@Autowiredprivate AccountService accountService;@Testpublic void run1(){accountService.pay("熊大","熊二",10.0);}
}

    http://www.dtcms.com/a/195602.html

    相关文章:

  1. Python爬虫(28)Python爬虫高阶:Selenium+Splash双引擎渲染实战与性能优化
  2. LangGraph 官方文档翻译 - 快速入门及示例教程(聊天、工具、记忆、人工干预、自定义状态、时间回溯)
  3. 【vue】适合大型项目的封装(接口,全局字典,表格表头)
  4. Python训练营打卡DAY27
  5. 金属加工液展|切削液展|2025上海金属加工液展览会
  6. 嵌入式开发书籍推荐
  7. 云服务器的运用自如
  8. GraphPad Prism项目的管理
  9. 动态规划(2):问题建模与状态设计
  10. Review --- 框架
  11. 实验-实现向量点积-RISC-V(计算机组成原理)
  12. C语言编程中的时间处理
  13. Cross-Site Scripting(XSS)
  14. Go语言之路————并发
  15. 一键清理功能,深度扫描本地存储数据
  16. 深度学习驱动下的目标检测技术:原理、算法与应用创新(三)
  17. memcached主主复制+keepalive
  18. Python多线程实战:提升并发效率的秘诀
  19. Linux常用命令42——tar压缩和解压缩文件
  20. Python 之类型注解
  21. Java项目使用Tomcat启动后JS文件中的中文乱码问题
  22. 彻底删除Docker容器中的环境变量
  23. 【Win32 API】 lstrcmpA()
  24. 第J1周:ResNet-50算法实战与解析
  25. entity线段材质设置
  26. let、var、const的区别
  27. 基于javaweb的SSM驾校管理系统设计与实现(源码+文档+部署讲解)
  28. 软考第六章知识点总结
  29. 如何安装cuda版本的pytorch
  30. PTN中的L2VPN与L3VPN技术详解