当前位置: 首页 > 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);}
}

    相关文章:

  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. 机器人为啥热衷“搞体育”,经济日报:是向加速融入日常生活发起的冲锋
  22. 美联储官员:美国经济增速可能放缓,现行关税政策仍将导致物价上涨
  23. 著名心血管病学专家李国庆教授逝世,享年63岁
  24. 中欧互动中的合作与分歧:务实需求将克服泛安全化的“政治钟摆”
  25. 德州国资欲退出三东筑工,后者大股东系当地房企东海集团
  26. 普京召开俄乌谈判筹备会议,拉夫罗夫、绍伊古等出席