spingboot银行案例

2023-11-09 12:59
文章标签 案例 银行 spingboot

本文主要是介绍spingboot银行案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.搭建环境

在这里插入图片描述

2.common

在这里插入图片描述

Account

public class Account implements Serializable {private Long id;private String cardno;private String password;private Double balance;private Long status;

Record

public class Record implements Serializable {private long id;private String cardno;private String transactiondate;private double expense;private double income;private double balance;private String transactiontype;private String remark;private String starttime;private String endtime;

AccountService

public interface AccountService {//1.登录Map<String,Object> login(Account account);//2.根据卡号查询余额Double findBalance(Account account);//3.修改密码String updatePWd(Account account);
}

RecordService

public interface RecordService {//1.转账String transferAccounts(String currentCardNo,String targetCardNo,Double money);//2.分页查询PageInfo<Map<String,Object>> showPage(Integer pageno, Record record);
}
3.provider结构

在这里插入图片描述

AccountMapper
public interface AccountMapper {//1.查询账号详情:根据卡号查,根据卡号密码查,根据卡号查余额Account findAccountInfo(Account account);//2.修改密码@Update("update account set password=#{password} where cardno#{cardno}")int updatePassword(Account account);//3.修改账号余额@Update("update account set balance=#{balance} where cardno#{cardno}")int updateBalance(Account account);
}
RecordMapper
public interface RecordMapper {//1.添加转出的交易记录@Insert("insert into record(cardno,transactiondate,expense,income,balance,transactiontype) values(#{cardno},now(),#{expense},0.0,#{balance},'转出')")int addExpenseRecord(Record record);//2.添加转入的交易记录@Insert("insert into record(cardno,transactiondate,expense,income,balance,transactiontype) values(#{cardno},now(),#{income},0.0,#{balance},'转入')")int addIncomeRecord(Record record);//3.根据交易日期分页查询,按照日期升序排列List<Map<String,Object>> showData(Record record);
}

AccountServiceImpl

@Service
@Transactional
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountMapper accountMapper;//登录@Overridepublic Map<String, Object> login(Account account) {Map<String, Object> map = new HashMap<String, Object>();//根据卡号查询Account loginuser = accountMapper.findAccountInfo(account);//判断卡号是否存在if (loginuser == null) {map.put("msg", "登录失败,你输入卡号不存在!");return map;}if(loginuser!=null){//判断密码是否正确if (!loginuser.getPassword().equals(account.getPassword())){map.put("msg","登录失败,您输入的密码不正确!");return map;}//判断账号是否被冻结if (loginuser.getStatus()==0){map.put("msg","登录失败,您的账号已冻结!");return map;}}//登录成功map.put("loginuser",loginuser);return map;}//查询余额@Overridepublic Double findBalance(Account account) {//根据卡号查询return accountMapper.findAccountInfo(account).getBalance();}//修改密码@Overridepublic String updatePWd(Account account) {String pwd = accountMapper.findAccountInfo(account).getPassword();if (!pwd.equals(account.getPassword())){return "旧密码输入错误!";}//修改accountMapper.updatePassword(account);return "success";}
}

RecordServiceImpl

public class RecordServiceImpl implements RecordService {@Autowiredprivate RecordMapper recordMapper;@Autowiredprivate AccountMapper accountMapper;//转账public String transferAccounts(String currentCardNo, String targetCardNo, Double money) {//查询目标账号Account targetAccount=accountMapper.findAccountInfo(new Account(targetCardNo,null,null));//判断目标卡号是否存在if(targetAccount==null){return "转账失败,目标账号不存在!";}//判断目标账号是否冻结if (targetAccount!=null && targetAccount.getStatus()==0){return "转账失败,目标账号已冻结";}//查询当前账号Account currentAccount=accountMapper.findAccountInfo(new Account(currentCardNo,null,null));//判断当前账号余额是否 >= 转账金额if (currentAccount!=null){if (currentAccount.getBalance()<money){return "转账失败,转出账号余额不足!";}}//当前账号余额减少currentAccount.setBalance(currentAccount.getBalance()-money);accountMapper.updateBalance(targetAccount);//目标账号余额增加targetAccount.setBalance(targetAccount.getBalance()+money);accountMapper.updateBalance(targetAccount);//当前账号转出交易记录Record currentAccountRecord=new Record(currentCardNo,money,null,currentAccount.getBalance());recordMapper.addExpenseRecord(currentAccountRecord);//目标账号转入交易记录Record targetAccountRecord=new Record(targetCardNo,null,money,targetAccount.getBalance());recordMapper.addIncomeRecord(targetAccountRecord);return "success";}//分页@Overridepublic PageInfo<Map<String, Object>> showPage(Integer pageno, Record record) {PageHelper.startPage(pageno,3);List<Map<String,Object>> list=recordMapper.showData(record);return new PageInfo<Map<String,Object>>(list);}
}

ProviderApplication

@MapperScan("cn.kgc.mapper")
@ImportResource("classpath:spring-provider.xml")
resource>mapper

AccountMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.AccountMapper"><select id="findAccountInfo" resultType="Account" parameterType="Account">SELECT * FROM account WHERE cardno=#{cardno}<if test="password!=null">AND password=#{password}</if></select>
</mapper>

RecordMapper.xml

<mapper namespace="cn.kgc.mapper.RecordMapper"><select id="showData" parameterType="Record" resultType="map">SELECT * from record WHERE cardno=#{cardno}<if test="starttime!=null and endtime!=nulll">AND transactiondate BETWEEN #{starttime} AND #{endtime}</if>ORDER BY transactiondate ASC</select>
</mapper>

application.properties

server.port=9090spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bankonline
spring.datasource.username=root
spring.datasource.password=123.mybatis.type-aliases-package=cn.kgc.vomybatis.mapper-locations=mapper/*.xmlpagehelper.helper-dialect=mysql

spring-provider.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:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="myprovider" /><!-- 使用zookeeper注册中心暴露服务地址,我的zookeeper是架在本地的 --><dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" timeout="60000"/><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20880" /><!-- 用户服务接口 --><dubbo:service interface="cn.kgc.service.AccountService" ref="accountService"/><bean id="accountService" class="cn.kgc.service.AccountServiceImpl"/><dubbo:service interface="cn.kgc.service.RecordService" ref="recordService"/><bean id="recordService" class="cn.kgc.service.RecordServiceImpl"/>
</beans>

4.consumer

CenterController

@RestController
public class CenterController {@Autowiredprivate AccountService accountService;@Autowiredprivate RecordService recordService;//登录@RequestMapping("/login.do")public Map<String,Object> login(Account account){return accountService.login(account);}//查询余额@RequestMapping("/balance.do")public Double findBalance(Account account){return accountService.findBalance(account);}//修改密码@RequestMapping("/pwd.do")public String updatePWd(Account account){return accountService.updatePWd(account);}//转账@RequestMapping("/ta.do")public String transferAccounts(String currentCardNo,String targetCardNo,Double money){return recordService.transferAccounts(currentCardNo,targetCardNo,money);}//分页@RequestMapping("/page.do")public PageInfo<Map<String,Object>> showPage(Integer pageno, Record record){return recordService.showPage(pageno,record);}
}

ConsumerApplication

@ImportResource("classpath:spring-consumer.xml")

rvice.transferAccounts(currentCardNo,targetCardNo,money);
}
//分页
@RequestMapping("/page.do")
public PageInfo<Map<String,Object>> showPage(Integer pageno, Record record){
return recordService.showPage(pageno,record);
}
}


ConsumerApplication```java
@ImportResource("classpath:spring-consumer.xml")

这篇关于spingboot银行案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/376237

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

六个案例搞懂mysql间隙锁

《六个案例搞懂mysql间隙锁》MySQL中的间隙是指索引中两个索引键之间的空间,间隙锁用于防止范围查询期间的幻读,本文主要介绍了六个案例搞懂mysql间隙锁,具有一定的参考价值,感兴趣的可以了解一下... 目录概念解释间隙锁详解间隙锁触发条件间隙锁加锁规则案例演示案例一:唯一索引等值锁定存在的数据案例二:

MySQL 表的内外连接案例详解

《MySQL表的内外连接案例详解》本文给大家介绍MySQL表的内外连接,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录表的内外连接(重点)内连接外连接表的内外连接(重点)内连接内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp