spring-银行转账系统
使用技术:Spring + mybatis+jsp+servlet
流程:1.导包
aopalliance.jar
asm-3.3.1.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.30.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
2.配置多个xml文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>spring_account</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--[1]连接数据库,获得数据源--><bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/mybatis4"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--[2]获得sqlsessionFactory--><bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="ds"></property><property name="typeAliasesPackage" value="com.bjsxt.pojo"></property></bean><!--[3]扫描mapper文件--><bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="factory"></property><property name="basePackage" value="com.bjsxt.mapper"></property></bean></beans>
applicationContext-service.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--配置注解的扫描--><context:component-scan base-package="com.bjsxt.serviceimpl"></context:component-scan><!--扫描事务注解--><tx:annotation-driven></tx:annotation-driven></beans>
applicationContext-tx.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--[4]配置声明式事务--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="ds"></property></bean><!--扫描 @Transactional--><tx:annotation-driven></tx:annotation-driven></beans>
3.写每个包中的代码:pojo ,mapper,service,serviceimpl,controller,jsp
pojo:
package com.bjsxt.pojo;import java.io.Serializable;public class Account implements Serializable {private int id;private String cno;private String pwd;private int money;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCno() {return cno;}public void setCno(String cno) {this.cno = cno;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", cno='" + cno + '\'' +", pwd='" + pwd + '\'' +", money=" + money +'}';}public Account(int id, String cno, String pwd, int money) {this.id = id;this.cno = cno;this.pwd = pwd;this.money = money;}public Account(){}
}
mapper:
package com.bjsxt.mapper;import com.bjsxt.pojo.Account;public interface AccountMapper {public Account select(String cno,String pwd,String money);public int update1(String outCno,int money);public int update2(String inCno,int money);
}
<?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="com.bjsxt.mapper.AccountMapper"><select id="select" resultType="account">select * from account<where><if test="param1!=null and param1!=''">cno=#{param1}</if><if test="param2!=null and param2!=''">and pwd=#{param2}</if><if test="param3!=null and param3!=''">and money>=#{param3}</if></where></select><update id="update1">update account set money=money-#{1} where cno=#{0}</update><update id="update2">update account set money=money+#{1} where cno=#{0}</update>
</mapper>
service:
package com.bjsxt.service;import com.bjsxt.pojo.Account;public interface AccountService {public Account findOne(String cno,String pwd,String money);public int upd(String outCno,String inCno,int money);
}
serviceimpl:
package com.bjsxt.serviceimpl;import com.bjsxt.mapper.AccountMapper;
import com.bjsxt.pojo.Account;
import com.bjsxt.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service("acc")
public class AccountServiceImpl implements AccountService {@AutowiredAccountMapper accountMapper;@Overridepublic Account findOne(String cno, String pwd, String money) {return accountMapper.select(cno,pwd,money);}@Override@Transactionalpublic int upd(String outCno, String inCno, int money) {int i1 = accountMapper.update1(outCno, money);int i2 = accountMapper.update2(inCno, money);if(i1>0&&i2>0){return 1;}return 0;}}
controller:
package com.bjsxt.controller;import com.bjsxt.pojo.Account;
import com.bjsxt.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet("/AccountController")
public class AccountController extends HttpServlet {AccountService as;@Overridepublic void init() throws ServletException {ApplicationContext app= WebApplicationContextUtils.getWebApplicationContext(getServletContext());as = app.getBean("acc", AccountService.class);}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String method = req.getParameter("method");if("checkUser".equals(method)){checkUser(req,resp);}else if("inOut".equals(method)){inOut(req,resp);}}protected void inOut (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.获取数据String cno = req.getParameter("cno");String cno2 = req.getParameter("cno2");int money =Integer.parseInt(req.getParameter("money")) ;//2.调业务处理int upd = as.upd(cno, cno2, money);//3.响应if(upd>0){resp.sendRedirect("/s_account/success.jsp");}else{resp.sendRedirect("/s_account/account.jsp");}}protected void checkUser (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//1.获得数据String cno = req.getParameter("cno");String pwd = req.getParameter("pwd");String money = req.getParameter("money");//2.调用业务Account one = as.findOne(cno, pwd, money);//响应if(one!=null){resp.getWriter().println("true");}else {resp.getWriter().println("false");}}}
account.jsp:
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/9/11Time: 17:02To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html>
<head><title>银行转账</title><script type="text/javascript" src="js/jquery-1.12.3.min.js"></script><script type="text/javascript">var flag1=false;var flag2=false;var flag3=false;//页面加载发起ajax请求$(function () {//在输入密码后失去焦点验证账号和密码是否正确$("#pwd").blur(function () {//发起ajax请求,参数为账号密码var cno=$("#cno").val();var pwd = $("#pwd").val();$.post("AccountController?method=checkUser","cno="+cno+"&pwd="+pwd, function (data) {if (data){flag1=true;$("#pwd_span").html("账号和密码一致").css("color","green");}else{$("#pwd_span").html("账号和密码不一致").css("color","red");}},"json")})//在输入金额后判断金额是否大于转出的金额$("#money").blur(function () {//发起ajax请求,参数为账号密码var cno=$("#cno").val();var pwd = $("#pwd").val();var money = $("#money").val();$.post("AccountController?method=checkUser","cno="+cno+"&pwd="+pwd+"&money="+money, function (data) {if (data){flag2=true;$("#money_span").html("合适的金额").css("color","green");}else{$("#money_span").html("金额不足").css("color","red");}},"json")})//判断收款账号是否正确/* $("#con2").blur(function () {var cno2=$("#cno2").val();var cno=$("#cno").val();alert(cno2);if (cno ==cno2){alert("转账账号和收入账号不能一样")}else{$.post("AccountController?method=checkUser","cno="+cno2, function (data) {if (data){$("#cno2_span").html("收款账号正常").css("color","green");}else{$("#cno2_span").html("收款账号不对").css("color","red");}},"json")}})
*///判断汇款人信息$("#cno2").blur(function () {var cno2=$("#cno2").val();var cno=$("#cno").val();if(cno==cno2){alert("!该账号和自己账号一致")}else{$.post("AccountController?method=checkUser","cno="+cno2,function (data1) {if(data1){flag3=true;$("#cno2_span").html("收款人信息正确").css("color","green");}else {$("#cno2_span").html("收款人信息错误").css("color","red");}},"json");}})})function change() {if (flag1&&flag2&&flag3){return true;//三个信息都正确,返回true ,再把true返回给onsubmit,onsubmit的true,false决定表达是否提交}return false;}</script>
</head>
<body>
<h3>银行转账系统</h3><form action="AccountController?method=inOut" method="post" onsubmit="return change()"><p>转账账号:<input type="text" id="cno" name="cno"/></p><p>转账密码:<input type="password" id="pwd" /><span id="pwd_span"></span></p><p>转账金额:<input type="text" id="money" name="money"/><span id="money_span"></span></p><p>收账账号:<input type="text" id="cno2" name="cno2"/><span id="cno2_span"></span></p><p>确认转账:<input type="submit" value="确认转账" /></p></form></body>
</html>
success.jsp:
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/9/11Time: 21:10To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h3>转账成功</h3>
</body>
</html>
spring-银行转账系统相关推荐
- python 3.6.5 模拟银行转账系统
1. 创建数据库并构造表 acctid用来标识账户 money用来标注账户的金额 可以查看一下表的结构 更加直观 2. 插入两个账户的数据并查看表中存储的内容 可以看到在1号户中存储了300,在2号中 ...
- 利用mysql模拟银行转账_实践项目七:模拟银行转账系统(python+mysql)
环境:ubutu 16.04 + python2.7 + mysql 5.7.19 最近学习python操作mysql数据库的知识,python访问数据库有自己的统一接口程序:Python DB AP ...
- Spring Framework系统架构
Spring Framework是Spring生态中最基本的项目,是其他项目的根基. Spring4.x之后,系统架构趋于稳定.Spring4.x的系统架构图如下:
- Spring cloud系统架构的淘宝客之一
前段时间申请了一个淘宝客的账号,本来想挣点烟钱,结果发现流量上不去,就此作罢. 当时是基于Spring boot开发的,正好最近想用Spring cloud搭个环境,所以打算代码拿出来,给各位JRS看 ...
- JAVA实验3:Java-MySQL实现银行转账系统
运行结果 实验中解决了用户登录时SQL注入问题,提高了系统的安全性 并且关闭了事务自动提交,开启了事务回滚功能,保证了用户资金的安全性 PowerDesigner16.5建模 数据库源码 drop t ...
- C#模拟银行转账系统
类库:BankModel 创建类:Bank using System; using System.Collections.Generic; using System.Linq; using Syste ...
- Spring cloud系统架构的淘宝客之二
引言 前段时间申请了一个淘宝客的账号,当时是基于Spring boot开发的,这次就主要介绍一下这个淘宝客demo 首先看一下运行效果,访问http://localhost:8080/index 就可 ...
- Spring boot系统拦截处理异常调转404/500页面
一.HandlerExceptionResolver接口 package com.lw.utils;import org.springframework.context.annotation.Conf ...
- 编写银行转账系统的数据库
//编写AccountDAO类,吧增删改查的实现方法,放在里面 package JDBC_Manager;import java.sql.*; import java.util.*; /*** 在此类 ...
- Spring AOPIOC初步引入-银行转账案例改造
Spring 概述 1.1Spring 简介 Spring 官⽅⽹址:http://spring.io/ Spring 是分层的 full-stack(全栈) 轻量级开源框架,以 IoC 和 AOP ...
最新文章
- 图解防雷技术基础知识
- 【2018.3.17】模拟赛之三-ssl1863jzoj1367 俄罗斯方块【模拟】
- Ubuntu12.04 内核树建立
- js统计html页面访问的次数6,JS综合篇--[总结]Web前端常用代码片段整理
- 号称36个月不卡顿!网友:就是有点贵
- adb命令刷机vivox20_求救VIVO X20的 ROOT可行的方法。
- bim建模的过程的几个要点
- 烧光20亿不够续命,快狗打车IPO找钱?
- 计算机突然蓝屏重启,最近电脑经常蓝屏重启。
- 获取apk运行占手机RAM大小
- 大数据hive篇--同比环比
- 小程序如何从0裂变开始获客?
- ToDesk企业版赋能零售行业,打造智慧门店
- 1字节等于多少bit
- 双鱼林java_双鱼林Java代码生成器
- 激光雷达与深度相机对比——以RS-LIDAR-16和Realsense D455为例
- Life Long Learning论文阅读记录之LwF
- 51单片机——红外遥控 C语言入门编程
- 九度OJ-1163:素数(未关联)
- FAQ是什么?如何高效地打造一个好的FAQ?
热门文章
- Android jetpack navigation
- Redis在Windows下的安装教程
- php 读取 excel 文件并上传数据库
- 基于高德地图SDK开发之地图显式
- 编程狸子的java road(一)
- Python开发一个APP居然如此之简单?老王自学三月就独立开发了!
- Oracle回收站及flashback drop(上)
- c语言程序中只能给指针赋,在c程序中,只能给指针变量赋NULL值和_____值. 答案:地址;指针...
- 自然语言处理之hanlp,Python调用与构建,分词、关键词提取、命名主体识别
- 洛谷算法题单:模拟与高精度例题(下)