隔了一段时间不用SSH框架开发了,有些生疏,所以就自己开发一个小系统,权当练练手,熟悉下该框架下的开发.新手写代码,不足之处,还请指教.
这个系统,我给它取个名叫网上银行系统,需求比较简单(练手用的嘛!),分为两大模块,普通用户模块和管理员模块.用户登录时,可以选择对应权限的模块登录,如下两图所示:
普通用户模块结构图:
花了一天半的时间开发完了客户功能模块.来瞧瞧:
示例: 用户存款,取款,转账,查询交易记录的代码
#数据模型层
TransactionLog.java
package com.sunline.entity;
/**
* TransactionLog entity. @author MyEclipse Persistence Tools
*/
public class TransactionLog implements java.io.Serializable {
// Fields
private Integer id;
private Integer userId;
private Integer otherId;
private Double trMoney;
private String datetime;
private String taType;
// Constructors
/** default constructor */
public TransactionLog() {
}
/** full constructor */
public TransactionLog(Integer userId, Integer otherId, Double trMoney, String datetime, String taType) {
this.userId = userId;
this.otherId = otherId;
this.trMoney = trMoney;
this.datetime = datetime;
this.taType = taType;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getOtherId() {
return this.otherId;
}
public void setOtherId(Integer otherId) {
this.otherId = otherId;
}
public Double getTrMoney() {
return this.trMoney;
}
public void setTrMoney(Double trMoney) {
this.trMoney = trMoney;
}
public String getDatetime() {
return this.datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public String getTaType() {
return this.taType;
}
public void setTaType(String taType) {
this.taType = taType;
}
}
TransactionLog.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.sunline.entity.TransactionLog" table="transaction_log" catalog="bank">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="userId" type="java.lang.Integer">
<column name="user_id" not-null="true" />
</property>
<property name="otherId" type="java.lang.Integer">
<column name="other_id" not-null="true" />
</property>
<property name="trMoney" type="java.lang.Double">
<column name="tr_money" precision="10" not-null="true" />
</property>
<property name="datetime" type="java.lang.String">
<column name="datetime" length="80" not-null="true" />
</property>
<property name="taType" type="java.lang.String">
<column name="ta_type" length="60" not-null="true" />
</property>
</class>
</hibernate-mapping>
#数据访问层
TransactionLogDao.java
package com.sunline.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.sunline.entity.TransactionLog;
@Repository("TransactionLogDao")
public class TransactionLogDao extends HibernateDaoSupport {
/*
* 添加交易记录
*/
public void AddTransLog(TransactionLog log){
System.out.println("成功添加交易记录");
this.getHibernateTemplate().save(log);
}
/*
* 获取交易记录
*/
@SuppressWarnings("unchecked")
public List<TransactionLog> findByUserId(int userId) {
String hsql="from TransactionLog where userId = :userId";
List<TransactionLog> result = (List<TransactionLog>) this.getHibernateTemplate().findByNamedParam(hsql,"userId",userId);
return result;
}
}
#业务逻辑层
TransactionLogBiz.java
package com.sunline.biz;
import java.util.List;
import com.sunline.dao.TransactionLogDao;
import com.sunline.entity.TransactionLog;
public class TransactionLogBiz {
TransactionLogDao transactionLogDao;
public TransactionLogDao getTransactionLogDao() {
return transactionLogDao;
}
public void setTransactionLogDao(TransactionLogDao transactionLogDao) {
this.transactionLogDao = transactionLogDao;
}
/*
* 添加交易记录
*/
public void AddTransLog(TransactionLog log){
transactionLogDao.AddTransLog(log);
}
/*
* 获取交易记录
*/
@SuppressWarnings("unchecked")
public List<TransactionLog> findByUserId(int userId) {
return transactionLogDao.findByUserId(userId);
}
}
#控制层
TransactionLogAction.java
package com.sunline.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sunline.biz.TransactionLogBiz;
import com.sunline.biz.UserBiz;
import com.sunline.entity.Page;
import com.sunline.entity.TransactionLog;
import com.sunline.entity.User;
public class TransactionLogAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer userId;
private Integer otherId;
private Double trMoney;
private String datetime;
private String taType;
private Page page;
private TransactionLog log;
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public TransactionLog getLog() {
return log;
}
public void setLog(TransactionLog log) {
this.log = log;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getOtherId() {
return otherId;
}
public void setOtherId(Integer otherId) {
this.otherId = otherId;
}
public Double getTrMoney() {
return trMoney;
}
public void setTrMoney(Double trMoney) {
this.trMoney = trMoney;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public String getTaType() {
return taType;
}
public void setTaType(String taType) {
this.taType = taType;
}
ActionContext contextq = ActionContext.getContext();
Map<String, Object> session = contextq.getSession();
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
TransactionLogBiz logBiz = (TransactionLogBiz) context.getBean("TransactionLogBiz");
UserBiz userBiz = (UserBiz) context.getBean("UserBiz");
/*
* 用户存款
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
TransactionLog log = new TransactionLog();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String datetime = df.format(new Date()).toString();
String taType = "存款";
log.setUserId(this.userId);
log.setOtherId(this.userId);
log.setDatetime(datetime);
log.setTrMoney(this.trMoney);
log.setTaType(taType);
logBiz.AddTransLog(log); //添加交易记录
User user = userBiz.findById(this.userId);
double user_balance = user.getUserBalance();
double AddMoney = this.trMoney;
double SUM = user_balance + AddMoney;
userBiz.UpdateUserBalance(SUM, this.userId); //添加存款到账户
return SUCCESS;
}
/*
* 客户取款
*/
public String Withdrawals() throws Exception{
TransactionLog log = new TransactionLog();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String datetime = df.format(new Date()).toString();
double money = 0-this.trMoney;
String taType = "取款";
log.setUserId(this.userId);
log.setOtherId(this.userId);
log.setDatetime(datetime);
log.setTrMoney(money);
log.setTaType(taType);
User user = userBiz.findById(this.userId);
double user_balance = user.getUserBalance();
double SubMoney = this.trMoney;
double Sub = user_balance - SubMoney;
if(Sub>=0){
logBiz.AddTransLog(log); //添加交易记录
userBiz.UpdateUserBalance(Sub, this.userId); //更改用户账户余额
session.remove("withdrawals"); //移除之前的提示
return SUCCESS;
}else{
session.put("withdrawals", "账户余额不足!");
return INPUT;
}
}
/*
* 客户转账
*/
@SuppressWarnings("unused")
public String Transfer() throws Exception{
TransactionLog log = new TransactionLog();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String datetime = df.format(new Date()).toString();
double money = 0-this.trMoney;
String taType = "转账";
log.setUserId(this.userId);
log.setOtherId(this.otherId);
log.setDatetime(datetime);
log.setTrMoney(money);
log.setTaType(taType);
User user = userBiz.findById(this.userId); //获取本人信息
User U = userBiz.findById(this.otherId); //获取转账到户人的信息
double user_balance = user.getUserBalance();
double SubMoney = this.trMoney;
double Sub = user_balance - SubMoney;
if(U!=null){
if(Sub>=0){
if(this.userId != this.otherId){
double other_balance = U.getUserBalance(); //更改转账目的方用户余额
double Sum = other_balance + this.trMoney;
logBiz.AddTransLog(log); //添加交易记录
userBiz.UpdateUserBalance(Sub, this.userId); //更改用户账户余额
userBiz.UpdateUserBalance(Sum, this.otherId);
session.remove("transfer"); //移除之前的提示
session.remove("person"); //移除之前的提示
session.remove("NoExist"); //移除之前的提示
return SUCCESS;
}else{
session.put("person", "不能给自己转账!");
session.remove("NoExist"); //移除之前的提示
return INPUT;
}
}
else{
session.put("transfer", "账户余额不足!");
return INPUT;
}
}
else{
session.put("NoExist", "不存在该用户!");
session.remove("person"); //移除之前的提示
return INPUT;
}
}
}
#视图层(部分代码)
transfer.jsp
<%@page import="com.sunline.entity.User"%>
<%@page import="com.sunline.biz.UserBiz"%>
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>我要转账</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="bootstrap-3.3.7/css/bootstrap.min.css">
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function check(){
var otherId = document.getElementById("otherId").value;
var trMoney = document.getElementById("trMoney").value;
if(otherId==""){
alert("请输入对方账户!");
return false;
}
if(trMoney==""){
alert("请输入转账金额!");
return false;
}
}
</script>
</head>
<body>
<div class="container-fluid row form-group">
<div class="panel panel-warning">
<div class="panel-heading">
<strong class="glyphicon glyphicon-retweet" style="text-align: left;">我要转账</strong>
</div>
<%
String name =request.getSession().getAttribute("user").toString();
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserBiz userBiz = (UserBiz) context.getBean("UserBiz");
User user = userBiz.findByName(name);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String Time = df.format(new Date()).toString();
%>
<div class="panel-body">
<form action="${pageContext.request.contextPath }/Transfer" method="post" οnsubmit="return check()">
<table class="table table-hover" border="1">
<tr style="height: 40px;" class="success">
<td>
<label style="width: 100px;text-align: right;padding-right: 15px;">转账时间:</label>
<input type="hidden" name="userId" value="<%=user.getUserId()%>"/>
<label><%=Time %></label>
</td>
</tr>
<tr style="height: 40px;">
<td>
<label style="width: 100px;text-align: right;padding-right: 15px;">对方账户:</label>
<input type="text" name="otherId" id="otherId" style="height: 32px;"/>
<span style="font-size: 16px;"><font color="red">${NoExist}</font></span>
<span style="font-size: 16px;"><font color="red">${person}</font></span><br/>
</td>
</tr>
<tr style="height: 40px;">
<td>
<label style="width: 100px;text-align: right;padding-right: 15px;">转账金额:</label>
<input type="text" name="trMoney" id="trMoney" style="height: 32px;"/>
<span style="font-size: 16px;"><font color="red">${transfer}</font></span><br/>
</td>
</tr>
<tr style="height: 40px;">
<td>
<label style="width: 100px;text-align: right;padding-right: 15px;"></label>
<button type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-saved"></span>转账
</button>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
TransactionRecords.jsp
<%@page import="com.sunline.entity.TransactionLog"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.sunline.entity.User"%>
<%@page import="com.sunline.biz.UserBiz"%>
<%@page import="com.sunline.biz.TransactionLogBiz"%>
<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TransactionRecords.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="bootstrap-3.3.7/css/bootstrap.min.css">
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid row form-group">
<div class="panel panel-info">
<div class="panel-heading">
<strong class="glyphicon glyphicon-th-list" style="text-align: left;">交易记录</strong>
</div>
<%
String name =request.getSession().getAttribute("user").toString();
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserBiz userBiz = (UserBiz) context.getBean("UserBiz");
TransactionLogBiz logBiz = (TransactionLogBiz) context.getBean("TransactionLogBiz");
User user = userBiz.findByName(name);
int user_id = user.getUserId();
List<TransactionLog> list = logBiz.findByUserId(user_id);
%>
<div class="panel-body">
<table class="table table-hover" border="1">
<tr style="height: 40px;" class="success">
<td>序号</td>
<td>对方账户</td>
<td>交易金额</td>
<td>交易类型</td>
<td>交易时间</td>
</tr>
<%
if (list == null || list.size() < 1) {
out.print("<tr><td colspan='5'>对不起,暂没有任何交易记录 !</td></tr>");
}
else {
for(int i=0; i<list.size(); i++){
TransactionLog log = list.get(i);
%>
<tr>
<td><%=i+1 %></td>
<td><%=log.getOtherId() %></td>
<td><%=log.getTrMoney() %></td>
<td><%=log.getTaType() %></td>
<td><%=log.getDatetime() %></td>
</tr>
<%
}
}
%>
</table>
</div>
</div>
</div>
</body>
</html>
#Spring 依赖注入的配置文件,Struts.xml等,篇幅有限就不写了