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中的最小执行单位,不能再进行分割.要么全部执行,要么全部 ...
随机推荐
- unittest使用数据驱动ddt
简介 ddt(data driven test)数据驱动测试:由外部数据集合来驱动测试用例,适用于测试方法不变,但需要大量变化的数据进行测试的情况,目的就是为了数据和测试步骤的分离 由于unittes ...
- redis 底层数据结构
简单动态字符串SDS 包含字符串长度,剩余可用长度,字符数组 用于Redis中所有的string存储 字典(map) 数组+链表形式,跟hashMap很像 链地址法解决hash冲突 rehash使用新 ...
- Web前端JS实现轮播图原理
实现轮播图有很多方式,但是html的结构都是一样的.本文使用了Jquery框架,Dom操作更加方便灵活 html部分: <div class="banner"> < ...
- java编程思想第四版第十三章字符串 总结
1. String和StringBulider的使用 通过书中介绍, 我们得知如下结论: 当使用+连接符将字符串进行拼接的时候, 编译器会进行自动优化为使用StringBuilder连接字符串. 当在 ...
- Salesforce学习之路(十三)Aura案例实战分析
Aura相关知识整合: Salesforce学习之路(十)Aura组件工作原理 Salesforce学习之路(十一)Aura组件属性<aura:attribute /> Salesforc ...
- Django 项目笔记
Django 环境的搭建 Django 安装 pip install django==2.1.4 Django 创建项目 django-admin startproject mysite Django ...
- 浅谈Node中的模块化
关于这篇文章早在去年年初的时候我就想写一片关于模块化的文章,但是推到现在才来完成也有很多好处,巩固之前对Node的理解.毕竟在我目前的项目中还没有一款项目是用到了Node开发,所以导致我对Node的一 ...
- react antd Table动态合并单元格
示例数据 原始数组 const data = [ { key: '0', name: 'John Brown', age:22, address: 'New York No. 1 Lake Park' ...
- css居中布局的几种方式
一.水平居中 若是行内元素,则直接给其父元素设置text-align: center即可 若是块级元素,则直接给该元素设置margin: 0 auto即可 若子元素包含浮动元素,则给父元素设置widt ...
- mysql--时区问题(时间差8个小时?修改Mysql 时区)
发现评论时间比本地时间晚8小时,原因:mysql默认时区选择了CST 解决办法: Ubuntu系统环境下: 1.检查mysql系统时区 进入mysql:mysql -u root -p mysql&g ...