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>

实现结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-11 08:56  sakura-yxf  阅读(406)  评论(0)    收藏  举报