生活随笔
收集整理的這篇文章主要介紹了
银行转账系统(Spring小项目)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
練習涉及:spring+jq+ajax+mybatis+springAOP
功能涉及:
1. 登錄(校驗)
2. 金額校驗
3. 驗收收款人信息
4. 轉賬
頁面預覽:
項目概覽:
使用jar包:
src:
create database bank
;
use bank
;
select
version() from dual
;create table t_account
(aid
int(10) not null auto_increment
,
apwd
varchar(100) not null
,
money
double,
uid
int(10) not null
,
primary
key(aid
));
select
* from t_account
;
insert into t_account
values(default,'123456',8548.60,1);
insert into t_account
values(default,'123456',10000.00,2);create table t_user
(uid
int(10) not null auto_increment
,
uname
varchar(100) not null
,
pwd
varchar(100) not null
,
primary
key(uid
));
insert into t_user
values(default,'張三','123');
insert into t_user
values(default,'李四','123');
select
* from t_user
;select
* from t_account a join t_user u on a
.aid
=u
.uid
;
package com
.han
.advice
;import org
.apache
.log4j
.Logger
;
import org
.springframework
.aop
.MethodBeforeAdvice
;import java
.lang
.reflect
.Method
;public class MyAfter implements MethodBeforeAdvice {@Overridepublic void before(Method method
, Object
[] objects
, Object o
) throws Throwable
{Logger logger
= Logger
.getLogger(MyAfter
.class);if (o
!=null
){logger
.debug(objects
[0]+"登錄成功");}}
}
package com
.han
.advice
;import org
.apache
.log4j
.Logger
;
import org
.springframework
.aop
.MethodBeforeAdvice
;import java
.lang
.reflect
.Method
;public class MyBefore implements MethodBeforeAdvice {@Overridepublic void before(Method method
, Object
[] objects
, Object o
) throws Throwable
{Logger logger
= Logger
.getLogger(MyBefore
.class);logger
.debug(objects
[0]+"發起了登錄請求");}
}
package com
.han
.controller
;import com
.han
.pojo
.Account
;
import com
.han
.service
.CheckAccountService
;
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;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(value
= "/checkAccount",loadOnStartup
= 2)
public class CheckAccountServlet extends HttpServlet {private CheckAccountService checkAccountService
;@Overridepublic void init() throws ServletException
{ApplicationContext ac
= new ClassPathXmlApplicationContext("applicationcontext.xml");checkAccountService
= (CheckAccountService
) ac
.getBean("checkAccountServiceImpl");}@Overrideprotected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{req
.setCharacterEncoding("utf-8");resp
.setCharacterEncoding("utf-8");resp
.setContentType("text/html;charset=utf-8");String methodName
= req
.getParameter("methodName");if ("checkOutInfo".equals(methodName
)){checkOutInfo(req
,resp
);}else if ("checkMoneyInfo".equals(methodName
)){checkMoneyInfo(req
,resp
);}else if ("checkInInfo".equals(methodName
)){checkInInfo(req
,resp
);}else if ("transferInfo".equals(methodName
)){transferInfo(req
,resp
);}else {System
.out
.println("沒有對應的邏輯方法"+methodName
);}}private void transferInfo(HttpServletRequest req
, HttpServletResponse resp
) throws IOException
{String outId
= req
.getParameter("outId");String inId
= req
.getParameter("inId");String money
= req
.getParameter("money");int i
= checkAccountService
.transferInfoService(outId
,inId
,money
);if (i
>0){resp
.sendRedirect(req
.getContextPath()+"/success.jsp");}else {resp
.sendRedirect(req
.getContextPath()+"/error.jsp");}}private void checkInInfo(HttpServletRequest req
, HttpServletResponse resp
) throws IOException
{String inId
= req
.getParameter("inId");String inName
= req
.getParameter("inName");Account account
= checkAccountService
.checkInInfoService(inId
,inName
);resp
.getWriter().write(account
!= null
? "true" : "false");}private void checkMoneyInfo(HttpServletRequest req
, HttpServletResponse resp
) throws IOException
{String outId
= req
.getParameter("outId");String money
= req
.getParameter("money");Account account
= checkAccountService
.checkMoneyInfoService(outId
,money
);resp
.getWriter().write(account
!= null
? "true" : "false");}private void checkOutInfo(HttpServletRequest req
, HttpServletResponse resp
) throws IOException
{String outId
= req
.getParameter("outId");String outPwd
= req
.getParameter("outPwd");Account account
= checkAccountService
.checkOutAccountInfoService(outId
,outPwd
);resp
.getWriter().write(account
!= null
? "true" : "false");}
}
package com
.han
.controller
;import com
.han
.pojo
.User
;
import com
.han
.service
.UserService
;
import com
.han
.service
.impl
.UserServiceImpl
;
import org
.springframework
.context
.ApplicationContext
;
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 javax
.servlet
.http
.HttpSession
;
import java
.io
.IOException
;@WebServlet(value
= "/userLogin",loadOnStartup
= 1)
public class UserServlet extends HttpServlet {private UserService us
;public void init(){ApplicationContext ac
= WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());us
= (UserService
) ac
.getBean("userServiceImpl");}@Overrideprotected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{req
.setCharacterEncoding("utf-8");resp
.setContentType("text/html;charset=utf-8");resp
.setCharacterEncoding("utf-8");String uname
= req
.getParameter("uname");String pwd
= req
.getParameter("pwd");User user
= us
.userLoginService(uname
, pwd
);HttpSession session
= req
.getSession();if (user
!=null
){session
.setAttribute("user",user
);resp
.sendRedirect(req
.getContextPath()+"/main.jsp");}else{session
.setAttribute("flag","userFail");resp
.sendRedirect(req
.getContextPath()+"/login.jsp");}}
}
package com
.han
.mapper
;import com
.han
.pojo
.Account
;
import org
.apache
.ibatis
.annotations
.Param
;
import org
.apache
.ibatis
.annotations
.Select
;
import org
.apache
.ibatis
.annotations
.Update
;public interface CheckAccountMapper {@Select("select * from t_account where aid=#{outId} and apwd=#{outPwd}")Account
checkAccountOutInfoMapper(@Param("outId") String outId
, @Param("outPwd") String outPwd
);@Select("select * from t_account where aid=#{outId} and money>=#{money}")Account
checkMoneyInfoMapper(@Param("outId") String outId
, @Param("money") String money
);@Select("select a.* from t_account a join t_user u on a.aid=u.uid where a.aid=#{inId} and u.uname=#{inName}")Account
checkInInfoMapper(@Param("inId") String inId
, @Param("inName") String inName
);@Update("update t_account set money=money-${money} where aid=#{outId}")int transferOut(@Param("outId")String outId
,@Param("money") String money
);@Update("update t_account set money=money+${money} where aid=#{inId}")int transferIn(@Param("inId")String inId
,@Param("money") String money
);
}
package com
.han
.mapper
;import com
.han
.pojo
.User
;
import org
.apache
.ibatis
.annotations
.Param
;
import org
.apache
.ibatis
.annotations
.Select
;public interface UserMapper {@Select("select * from t_user where uname=#{uname} and pwd=#{pwd}")User
userLoginMapper(@Param("uname") String uname
, @Param("pwd") String pwd
);}
package com
.han
.pojo
;import java
.util
.Objects
;public class Account {private Integer aid
;private String apwd
;private Double money
;private Integer uid
;private User user
;@Overridepublic String
toString() {return "Account{" +"aid=" + aid
+", apwd='" + apwd
+ '\'' +", money=" + money
+", uid=" + uid
+", user=" + user
+'}';}@Overridepublic boolean equals(Object o
) {if (this == o
) return true;if (o
== null
|| getClass() != o
.getClass()) return false;Account account
= (Account
) o
;return Objects
.equals(aid
, account
.aid
) &&Objects
.equals(apwd
, account
.apwd
) &&Objects
.equals(money
, account
.money
) &&Objects
.equals(uid
, account
.uid
) &&Objects
.equals(user
, account
.user
);}@Overridepublic int hashCode() {return Objects
.hash(aid
, apwd
, money
, uid
, user
);}public Integer
getAid() {return aid
;}public void setAid(Integer aid
) {this.aid
= aid
;}public String
getApwd() {return apwd
;}public void setApwd(String apwd
) {this.apwd
= apwd
;}public Double
getMoney() {return money
;}public void setMoney(Double money
) {this.money
= money
;}public Integer
getUid() {return uid
;}public void setUid(Integer uid
) {this.uid
= uid
;}public User
getUser() {return user
;}public void setUser(User user
) {this.user
= user
;}public Account() {}public Account(Integer aid
, String apwd
, Double money
, Integer uid
, User user
) {this.aid
= aid
;this.apwd
= apwd
;this.money
= money
;this.uid
= uid
;this.user
= user
;}
}
package com
.han
.pojo
;import java
.util
.Objects
;public class User {private Integer uid
;private String uname
;private String pwd
;@Overridepublic boolean equals(Object o
) {if (this == o
) return true;if (o
== null
|| getClass() != o
.getClass()) return false;User user
= (User
) o
;return Objects
.equals(uid
, user
.uid
) &&Objects
.equals(uname
, user
.uname
) &&Objects
.equals(pwd
, user
.pwd
);}@Overridepublic int hashCode() {return Objects
.hash(uid
, uname
, pwd
);}@Overridepublic String
toString() {return "User{" +"uid=" + uid
+", uname='" + uname
+ '\'' +", pwd='" + pwd
+ '\'' +'}';}public Integer
getUid() {return uid
;}public void setUid(Integer uid
) {this.uid
= uid
;}public String
getUname() {return uname
;}public void setUname(String uname
) {this.uname
= uname
;}public String
getPwd() {return pwd
;}public void setPwd(String pwd
) {this.pwd
= pwd
;}public User() {}public User(Integer uid
, String uname
, String pwd
) {this.uid
= uid
;this.uname
= uname
;this.pwd
= pwd
;}
}
package com
.han
.service
.impl
;import com
.han
.mapper
.CheckAccountMapper
;
import com
.han
.pojo
.Account
;
import com
.han
.service
.CheckAccountService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;@Service
public class CheckAccountServiceImpl implements CheckAccountService {@Autowiredprivate CheckAccountMapper checkAccountMapper
;@Overridepublic Account
checkOutAccountInfoService(String outId
, String outPwd
) {return checkAccountMapper
.checkAccountOutInfoMapper(outId
,outPwd
);}@Overridepublic Account
checkMoneyInfoService(String outId
, String money
) {return checkAccountMapper
.checkMoneyInfoMapper(outId
,money
);}@Overridepublic Account
checkInInfoService(String inId
, String inName
) {return checkAccountMapper
.checkInInfoMapper(inId
,inName
);}@Overridepublic int transferInfoService(String outId
, String inId
, String money
) {int i
= checkAccountMapper
.transferOut(outId
,money
);i
+= checkAccountMapper
.transferIn(inId
,money
);return i
;}
}
package com
.han
.service
.impl
;import com
.han
.mapper
.UserMapper
;
import com
.han
.pojo
.User
;
import com
.han
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper
;public UserServiceImpl(UserMapper userMapper
) {this.userMapper
= userMapper
;}@Overridepublic User
userLoginService(String uname
, String pwd
) {return userMapper
.userLoginMapper(uname
,pwd
);}
}
package com
.han
.service
;import com
.han
.pojo
.Account
;public interface CheckAccountService {Account
checkOutAccountInfoService(String outId
, String outPwd
);Account
checkMoneyInfoService(String outId
, String money
);Account
checkInInfoService(String inId
, String inName
);int transferInfoService(String outId
, String inId
, String money
);
}
package com
.han
.service
;import com
.han
.pojo
.User
;public interface UserService {User
userLoginService(String uname
, String pwd
) ;
}
配置文件:
<?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
:property
-placeholder location
="classpath:db.properties"/><!--配置注解掃描
--><context
:component
-scan base
-package="com.han.service.impl"></context
:component
-scan
><!-- 配置數據源bean
--><bean id
="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 基本屬性 url、user、password
--><property name
="driverClassName" value
="${mysql.driver}" /><property name
="url" value
="${mysql.url}" /><property name
="username" value
="${mysql.username}" /><property name
="password" value
="${mysql.password}" /></bean
><!-- 配置工廠bean
--><bean id
="factory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name
="dataSource" ref
="dataSource" /></bean
><!-- 配置mapper掃描bean
--><bean id
="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name
="sqlSessionFactoryBeanName" value
="factory" /><property name
="basePackage" value
="com.han.mapper" /></bean
><!--<
;!&ndash
; 配置業務層bean
&ndash
;>
;<
;!&ndash
;用戶登錄
&ndash
;>
;<bean id
="us" class="com.han.service.impl.UserServiceImpl"><property name
="userMapper" ref
="userMapper"></property
></bean
><
;!&ndash
;配置信息校驗
&ndash
;>
;<bean id
="checkAccountService" class="com.han.service.impl.CheckAccountServiceImpl"><property name
="checkAccountMapper" ref
="checkAccountMapper"></property
></bean
>-->
<!-- <
;!&ndash
; 配置通知bean
&ndash
;>
;-->
<!-- <bean id
="before" class="com.han.advice.MyBefore"></bean
>-->
<!-- <bean id
="after" class="com.han.advice.MyAfter"></bean
>-->
<!-- <
;!&ndash
; 配置AOP組裝規則
&ndash
;>
;-->
<!-- <aop
:config
>-->
<!-- <aop
:pointcut id
="mp" expression
="execution(* com.han.service.impl.UserServiceImpl.userLoginService(String,String))"/>-->
<!-- <aop
:advisor advice
-ref
="before" pointcut
-ref
="mp"></aop
:advisor
>-->
<!-- <aop
:advisor advice
-ref
="after" pointcut
-ref
="mp"></aop
:advisor
>-->
<!-- </aop
:config
>--><!--SpringTX的事務管理
--><bean id
="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name
="dataSource" ref
="dataSource"></property
></bean
><!--配置事務管理方法
--><tx
:advice id
="advice" transaction
-manager
="transactionManager"><tx
:attributes
><tx
:method name
="sel*"/><tx
:method name
="ins*"/><tx
:method name
="up*"/><tx
:method name
="del*"/><tx
:method name
="transfer*"/></tx
:attributes
></tx
:advice
><!--配置事務管理切面
--><aop
:config
><aop
:pointcut id
="mp" expression
="execution(* com.han.service.impl.*.*(..))"/><!--增加事務通知
--><aop
:advisor advice
-ref
="advice" pointcut
-ref
="mp"></aop
:advisor
></aop
:config
>
</beans
>
mysql
.driver
=com
.mysql
.jdbc
.Driver
mysql
.url
=jdbc
:mysql
://localhost
:3306/[數據庫名
]
mysql
.username
=root
mysql
.password
=[數據庫密碼
]
log4j
.rootLogger
=infolog4j
.logger
.com
.han
.mapper
=debug
, CONSOLE
,LOGFILE
log4j
.logger
.com
.han
.service
.Impl
=debug
, CONSOLE
,LOGFILE
log4j
.appender
.CONSOLE
=org
.apache
.log4j
.ConsoleAppender
log4j
.appender
.CONSOLE
.layout
=org
.apache
.log4j
.PatternLayout
log4j
.appender
.CONSOLE
.layout
.ConversionPattern
=- %c
-%d
-%m
%nlog4j
.appender
.LOGFILE
=org
.apache
.log4j
.FileAppender
log4j
.appender
.LOGFILE
.File
=E
:\axis
.log
log4j
.appender
.LOGFILE
.Append
=true
log4j
.appender
.LOGFILE
.layout
=org
.apache
.log4j
.PatternLayout
log4j
.appender
.LOGFILE
.layout
.ConversionPattern
=- %c
-%d
-%m
%n
web.xml:
<?xml version
="1.0" encoding
="UTF-8"?>
<web
-app xmlns
="http://java.sun.com/xml/ns/javaee"xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"xsi
:schemaLocation
="http
://java
.sun
.com
/xml
/ns
/javaeehttp
://java
.sun
.com
/xml
/ns
/javaee
/web
-app_3_0
.xsd"version
="3.0"><!--配置全局參數:記錄Spring配置文件名
--><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
>
前端代碼:
login.jsp:
<%String path
= request
.getContextPath();String basePath
= request
.getScheme() + "://" + request
.getServerName() + ":" + request
.getServerPort() + path
+ "/";
%>
<%@ taglib prefix
="c" uri
="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head><base href
="<%=basePath %>"/><title>登錄頁面
</title
>
</head
>
<body><h3 align
="center">歡迎登錄銀行轉賬系統
</h3
><hr><div style
="width: 400px;margin: auto"><c
:if test
="${sessionScope.flag=='userFail'}"><font color
="red" size
="20px">用戶名或密碼錯誤
</font
></c
:if><c
:remove var
="flag" scope
="session"/><form action
="${pageContext.request.contextPath}/userLogin" method
="post"><table style
="margin: auto;margin-top: 30px" cellpadding
="10px"><tr><td>用戶名:
</td
><td><input type
="text" name
="uname" value
=""></td
></tr
><tr><td>密碼:
</td
><td><input type
="password" name
="pwd" value
=""></td
></tr
><tr><td colspan
="2"><input type
="submit" value
="點擊登錄"></td
><td></td
></tr
></table
></form
></div
>
</body
>
</html
>
main.jsp:
<%String path
= request
.getContextPath();String basePath
= request
.getScheme() + "://" + request
.getServerName() + ":" + request
.getServerPort() + path
+ "/";
%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head><base href
="<%=basePath %>"/><title>銀行轉賬系統
</title
><%--引入jQuery
--%><script type
="text/javascript" src
="js/j.js"></script
><%--聲明js代碼域
--%><script type
="text/javascript">$
(function
() {window
.setInterval(function
() {var colors
= ('#'+('00000'+(Math
.random()*0x1000000<<0).toString(16)).slice(-6));$
("#title").css("color",colors
);},1000)})$
(function
() {$
("#outPwd").blur(function
() {$
.post("checkAccount",{outId
:$
("#outId").val(),outPwd
:$
("#outPwd").val(),methodName
:"checkOutInfo"},function
(data
) {if (eval(data
)){$
("#outSpan").html("√").css("color","green").addClass("success").removeClass("error");}else{$
("#outSpan").html("×").css("color","red").addClass("error").removeClass("success");}})})})$
(function
() {$
("#money").blur(function
() {$
.post("checkAccount",{outId
:$
("#outId").val(),money
:$
("#money").val(),methodName
:"checkMoneyInfo"},function
(data
) {if (eval(data
)){$
("#moneySpan").html("√").css("color","green").addClass("success").removeClass("error");}else{$
("#moneySpan").html("×").css("color","red").addClass("error").removeClass("success");}})})})$
(function
() {$
("#inName").blur(function
() {$
.post("checkAccount",{inId
:$
("#inId").val(),inName
:$
("#inName").val(),methodName
:"checkInInfo"},function
(data
) {if (eval(data
)){$
("#inNameSpan").html("√").css("color","green").addClass("success").removeClass("error");}else{$
("#inNameSpan").html("×").css("color","red").addClass("error").removeClass("success");}})})})$
(function
() {$
("#btn").click(function
() {if ($
(".success").length
==3){$
("#fm").submit();}else{alert("請填寫正確的賬戶信息")}})})</script
>
</head
>
<body><h3 align
="center"><%--跑馬燈
--%><marquee width
=50% behavior
=alternate align
=middle"
><font id
="title">歡迎$
{sessionScope
.user
.uname
}登錄銀行轉賬系統
</font
></marquee
></h3
><hr><div style
="width: 400px;margin:auto;"><form action
="checkAccount" method
="post" id
="fm"><input type
="hidden" name
="methodName" value
="transferInfo"><table style
="margin:auto;margin-top: 30px;" cellpadding
="10px"><tr><td>轉賬賬戶:
</td
><td><input type
="text" name
="outId" id
="outId" value
=""></td
></tr
><tr><td>轉賬賬戶密碼:
</td
><td><input type
="password" id
="outPwd" value
=""><span id
="outSpan"></span
></td
></tr
><tr><td>金額:
</td
><td><input type
="text" name
="money" id
="money" value
=""><span id
="moneySpan"></span
></td
></tr
><tr><td>收款賬號:
</td
><td><input type
="text" id
="inId" name
="inId" value
=""></td
></tr
><tr><td>收款人姓名:
</td
><td><input type
="text" id
="inName" value
=""><span id
="inNameSpan"></span
></td
></tr
><tr><td colspan
="2"><input type
="button" value
="開始轉賬" id
="btn"></td
></tr
></table
></form
></div
>
</body
>
</html
>
總結
以上是生活随笔為你收集整理的银行转账系统(Spring小项目)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。