先是数据库(我用的orcal)
create table ACCOUNTINFO
(
id number(11) primary key not null,
account_number varchar2(16) not null,
account_password varchar2(6) not null,
account_money number(11) not null,
account_stutus number(11) not null--0可用,1不可用
);
create sequence seq_accountinfo;
insert into ACCOUNTINFO values(seq_accountinfo.nextval,'6221000000000001','123456',6000,0);
insert into ACCOUNTINFO values(seq_accountinfo.nextval,'6221000000000002','222222',8000,0);
insert into ACCOUNTINFO values(seq_accountinfo.nextval,'6221000000000003','333333',1000,1);
insert into ACCOUNTINFO values(seq_accountinfo.nextval,'6221000000000004','444444',2000,0);
action类:
package cn.account.action;
import cn.account.bean.AccountInfo;
import cn.account.service.AccoutInfoService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 核心控制器
*
*
*
*/
public class AccountInfoAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -8436508189819715435L;
private String accountNumber; // 卡号
private String accountPwd; // 密码
private AccountInfo account; // 卡
private Integer money;// 取钱数
private String message; // 消息
private AccoutInfoService accountService; // 业务
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountPwd() {
return accountPwd;
}
public void setAccountPwd(String accountPwd) {
this.accountPwd = accountPwd;
}
public AccountInfo getAccount() {
return account;
}
public void setAccount(AccountInfo account) {
this.account = account;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void setAccountService(AccoutInfoService accountService) {
this.accountService = accountService;
}
public String validateAccount() {
account = this.accountService.getAccountByNumPwd(accountNumber, accountPwd);
if (account != null) {
ActionContext.getContext().getSession().put("account", account);
return Action.SUCCESS;
} else {
this.message = "卡号或密码错误!";
return Action.INPUT;
}
}
public String updateAccount(){
account = this.accountService.updateAccount(account, money);
if(account != null){
return Action.SUCCESS;
}else{
this.message = "取款金额不能超过余额!";
return Action.INPUT;
}
}
}
bean类:
package cn.account.bean;
/**
* AccountInfo entity. @author MyEclipse Persistence Tools
*/
public class AccountInfo implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 2800588973146481870L;
private Integer id;
private String accountNumber;
private String accountPassword;
private Integer accountMoney;
private Integer accountStutus;
// Constructors
/** default constructor */
public AccountInfo() {
}
/** full constructor */
public AccountInfo(String accountNumber, String accountPassword,
Integer accountMoney, Integer accountStutus) {
this.accountNumber = accountNumber;
this.accountPassword = accountPassword;
this.accountMoney = accountMoney;
this.accountStutus = accountStutus;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccountNumber() {
return this.accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountPassword() {
return this.accountPassword;
}
public void setAccountPassword(String accountPassword) {
this.accountPassword = accountPassword;
}
public Integer getAccountMoney() {
return this.accountMoney;
}
public void setAccountMoney(Integer accountMoney) {
this.accountMoney = accountMoney;
}
public Integer getAccountStutus() {
return this.accountStutus;
}
public void setAccountStutus(Integer accountStutus) {
this.accountStutus = accountStutus;
}
}
dao类:
package cn.account.dao;
import cn.account.bean.AccountInfo;
/**
* 数据库访问接口
*
*/
public interface AccountInfoDao {
/**
* 根据卡号和密码得到卡
* @param accountNumber
* @param accountPwd
* @return 卡对象AccountInfo
*/
public AccountInfo getAccountByNumPwd(String accountNumber,String accountPwd);
/**
* 修改卡内数据
* @param account
* @return 卡对象AccountInfo
*/
public AccountInfo updateAccount(AccountInfo account,Integer money);
}
daoimpl类:
package cn.account.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.account.bean.AccountInfo;
import cn.account.dao.AccountInfoDao;
/**
* 数据库访问接口实现类
*
*/
public class AccountInfoDaoImpl extends HibernateDaoSupport implements
AccountInfoDao {
public AccountInfo getAccountByNumPwd(String accountNumber,
String accountPwd) {
String hql = "from AccountInfo where accountNumber=" + accountNumber + " and accountPassword=" + accountPwd;
return this.getHibernateTemplate().find(hql).size() == 0 ? null : (AccountInfo)this.getHibernateTemplate().find(hql).get(0);
}
public AccountInfo updateAccount(AccountInfo account,Integer money) {
Integer newMoney = 0;
if(money > account.getAccountMoney())
return null;
newMoney = account.getAccountMoney() - money;
account.setAccountMoney(newMoney);
return account;
}
}
|
|