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/javaee
         http://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/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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://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/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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://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/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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://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;
    }

    @Override
    public 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 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="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 {


    @Autowired
    AccountMapper accountMapper;
    @Override
    public Account findOne(String cno, String pwd, String money) {
        return accountMapper.select(cno,pwd,money);
    }

    @Override
    @Transactional
    public 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;
    @Override
    public void init() throws ServletException {
        ApplicationContext app= WebApplicationContextUtils.getWebApplicationContext(getServletContext());
         as = app.getBean("acc", AccountService.class);

    }

    @Override
    protected 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: Administrator
  Date: 2019/9/11
  Time: 17:02
  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">
        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: Administrator
  Date: 2019/9/11
  Time: 21:10
  To 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>

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值