Sring+mybatis模拟银行转账

实体类:
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 Account(int id, String cno, String pwd, int money) {
this.id = id;
this.cno = cno;
this.pwd = pwd;
this.money = money;
}
public Account(){};
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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", cno='" + cno + '\'' +
", pwd='" + pwd + '\'' +
", money=" + money +
'}';
}
}
Mapper层:
接口:
package com.bjsxt.mapper;
import com.bjsxt.pojo.Account;
import org.apache.ibatis.annotations.Param;
public interface AccountMapper {
Account selaccount(@Param("cno") String cno,@Param("pwd") String pwd,@Param("money") int money);
int updateaccount(@Param("cno") String cno,@Param("money") int money);
int updateaccount2(@Param("cno2") String cno2,@Param("money") int money);
}
Mapper.xml:(在判断前台数据时用ajax判断数据 我们需要用动态查询)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.AccountMapper">
<select id="selaccount" resultType="Account" parameterType="Account">
select * from account
<where>
<if test="cno!=null and cno!=''">
cno=#{cno}
</if>
<if test="pwd!=null and pwd!=''">
and pwd=#{pwd}
</if>
<if test="money>0">
and money>=#{money}
</if>
</where>
</select>
<update id="updateaccount" parameterType="Account">
update account set money=money-#{money} where cno=#{cno}
</update>
<update id="updateaccount2" parameterType="Account">
update account set money=money+#{money} where cno=#{cno2}
</update>
</mapper>
Service接口:
package com.bjsxt.service;
import com.bjsxt.pojo.Account;
public interface AccountService {
public Account findaccout( String cno, String pwd, int money);
int updateaccount(String cno, String cno2, int money);
}
Service实现类
package com.bjsxt.service.impl;
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;
@Service("asi")
public class AccountServiceImpl implements AccountService {
@Autowired
AccountMapper accountMapper;
@Override
public Account findaccout(String cno, String pwd, int money) {
Account selaccount = accountMapper.selaccount(cno, pwd, money);
return selaccount;
}
@Override
public int updateaccount(String cno, String cno2, int money) {
int updateaccount = accountMapper.updateaccount(cno, money);
int i = accountMapper.updateaccount2(cno2, money);
return updateaccount&i;
}
}
Spring核心配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--连接数据库-->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver1}"/>
<property name="url" value="${url1}"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--获得sqlsession-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"/>
</bean>
<!--扫描mapper文件-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"/>
<property name="basePackage" value="com.bjsxt.mapper"/>
</bean>
<!--扫描业务层注解-->
<context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
</beans>
前台(用ajax发送请求数据,及数据流转)
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/9/11
Time: 17:01
To 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">
$(function () {
$("#pwd").blur(function () {
//获得请求参数
var cno = $("#cno").val();
var pwd = $("#pwd").val();
//发起ajax请求
$.post("AccountServlet?method=CheckAccount","cno="+cno+"&pwd="+pwd,function (data1) {
if (data1){
$("#pwd_span").html("√ 汇款人信息正确").css("color","green");
}else {
$("#pwd_span").html("× 汇款人信息有误!!!").css("color","red");
}
},"json")
})
$("#money").blur(function () {
//获得请求参数
var cno = $("#cno").val();
var pwd = $("#pwd").val();
var money = $("#money").val();
//发起ajax请求
$.post("AccountServlet?method=CheckAccount","cno="+cno+"&pwd="+pwd+"&money="+money,function (data2) {
if (data2){
$("#money_span").html("√ 金额足够").css("color","green");
}else {
$("#money_span").html("× 金额不足!!!").css("color","red");
}
},"json")
})
$("#cno2").blur(function () {
var cno = $("#cno").val();
var cno2=$("#cno2").val();
if(cno==cno2){
alert("汇款人和收款人不能是同一个人!!!");
}else {
//发送ajax请求
$.post("AccountServlet?method=CheckAccount","cno="+cno2,function (data3) {
if (data3){
$("#cno2_span").html("√ 汇款人信息正确").css("color","green");
}else {
$("#cno2_span").html("× 汇款人信息有误!!!").css("color","red");
}
},"json")
}
})
})
</script>
</head>
<body>
<div>
<form action="AccountServlet?method=inOutMoney" method="post">
<p>
转账账号:<input type="text" name="cno" id="cno"/>
</p>
<p>
转账密码:<input type="password" name="pwd" id="pwd"/>
<span id="pwd_span"></span>
</p>
<p>
转账金额:<input type="text" name="money" id="money"/>
<span id="money_span"></span>
</p>
<p>
收款账号:<input type="text" name="cno2" id="cno2"/>
<span id="cno2_span"></span>
</p>
<p>
<input type="submit" value="转账"/>
</p>
</form>
</div>
</body>
</html>
实现结果:




Sring+mybatis模拟银行转账的更多相关文章
- MySQL_(Java)【连接池】使用DBCP简单模拟银行转账事物
dbcp下载 传送门 Commons Pool下载 传送门 Commons log下载 传送门 MySQL_(Java)[事物操作]使用JDBC模拟银行转账向数据库发起修改请求 传送门 MySQL_( ...
- Spring中 使用注解+c3p0+事物 《模拟银行转账》
使用注解的方式 模拟转账 要么都成功 要么都失败 !保持一致性! 准备工作: jar包: 需要的类: UserDao: package com.hxzy.spring.c3p0.Dao ...
- MySQL_(Java)【事物操作】使用JDBC模拟银行转账向数据库发起修改请求
MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...
- 【Spring】Spring的事务管理 - 2、声明式事务管理(实现基于XML、Annotation的方式。)
声明式事务管理 文章目录 声明式事务管理 基于XML方式的声明式事务 基于Annotation方式的声明式事务 简单记录 - 简单记录-Java EE企业级应用开发教程(Spring+Spring M ...
- Spring企业级程序设计 • 【第4章 Spring持久化层和事务管理】
全部章节 >>>> 本章目录 4.1 配置数据源资源 4.1.1 JdbcTemplate介绍 4.1.2通过ComboPooledDataSource创建数据源 4.1. ...
- Code First开发系列之管理并发和事务
返回<8天掌握EF的Code First开发>总目录 本篇目录 理解并发 理解积极并发 理解消极并发 使用EF实现积极并发 EF的默认并发 设计处理字段级别的并发应用 实现RowVersi ...
- UnitOfWork以及其在ABP中的应用
Unit Of Work(UoW)模式在企业应用架构中被广泛使用,它能够将Domain Model中对象状态的变化收集起来,并在适当的时候在同一数据库连接和事务处理上下文中一次性将对象的变更提交到数据 ...
- Code First开发系列之管理并发和事务(转)
转自:http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html 返回<8天掌握EF的Code First开发&g ...
- Oracle基础 事务
一.事务 事务就是业务上的一个逻辑单元,它能够保证其中对数据所有的操作,要么全部成功,要么全部失败. 二.事务的特性: 1.原子性:事务是SQL中的最小执行单位,不能再进行分割.要么全部执行,要么全部 ...
随机推荐
- 淘宝小练习#css
* { margin: 0; padding: 0; } a { text-decoration: none; } .box { background: #f4f4f4; } /* 头部样式STAR ...
- .net 上传文件:超过了最大请求长度
修改 web.config: 该方法是.net框架限制 添加: <system.web> ... ... <httpRuntime ... maxRequestLength=&q ...
- SqlServer2005 查询 第五讲 top
今天我们来说sql命令中得参数top top top[ 最前面若干个记录,专属于SqlServer2005的语法,不可移植到其他库.oracle中是用rownum<6来实现输出前5行记录.] 下 ...
- Spring Boot Actuator监控使用详解
在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...
- ESP8266 使用AT指令
ESP8266 使用AT指令 问题:串口调试工具输入AT指令没返回结果 分析板子有两种模式 下载模式(默认) 运行模式 解决办法: 方法一:按下板子上的 RST 键位 方法二:使用 [安信可串口调试工 ...
- 插入订单并且输出订单号的sql存储过程
--插入订单-- create proc InsertOrders ( @OrderNumber varchar(300), @OrderState varchar(30), @OrderType v ...
- cn_windows虚拟机配置
1.打开“VMware”,点击“主页”,点“创建新的虚拟机”: 2.会弹出一个“新建虚拟机向导”,类型选择“典型”,点击“下一步”: 3.选择“稍后安装操作系统”,点击“下一步”: 4.选择“Micr ...
- PHP产生不重复随机数的5个方法总结
无论是Web应用,还是WAP或者移动应用,随机数都有其用武之地.在最近接触的几个小项目中,我也经常需要和随机数或者随机数组打交道,所以,对于PHP如何产生不重复随机数常用的几种方法小结一下 无论是We ...
- 实现websocket 主动消息推送,用laravel+Swoole
近来有个需求:想实现一个可以主动触发消息推送的功能,这个可以实现向模板消息那个,给予所有成员发送自定义消息,而不需要通过客户端发送消息,服务端上message中监听传送的消息进行做相对于的业务逻辑. ...
- 十、CSR8670的DFU功能[补充]
前一篇转载的博文很清楚,全面的介绍了DFU功能的实现步骤.关于DFU功能,你还需要知道以下信息: 一.image.fs,firmware,loader,psr之间的关系 图1-1 image.fs示意 ...