用spring写的一个模仿"手机充值+查话费+销户"的一个小例子,供spring入门者参考.
三层结构:
表现层(view) +业务层(business)+数据访问层(dao)
首先定义业务层的接口:
IService.java
package business;
import po.cardPO;
import po.phonePO;

public interface IService ...{
//查询余额
public double queryBalance(phonePO phone);
//充值
public boolean addMoney(phonePO phone,cardPO card);
//销户
public boolean delAccount(phonePO phone);
//判断是否是合法用户
public boolean isUser(phonePO phone);
}
定义dao层的接口:
IMobileDAO.java
package dao;
import po.cardPO;
import po.phonePO;

public interface IMobileDAO ...{
//判断是不是合法手机用户
public boolean isPhoneExist(phonePO phone);
//判断充值卡是否有效
public boolean isCardExist(cardPO card);
//充值
public boolean addMoney(phonePO phone);
//查询余额
public double queryBalance(phonePO phone);
//查询充值卡金额
public double queryCardMoney(cardPO card);
//销户
public boolean delAccount(phonePO phone);
}
两个po对象:
phonePO.java 手机信息:
package po;

public class phonePO ...{
//手机号
private String phonenb;
//手机密码
private String password;
//话费
private double money;
//状态标志 o表示已注销,1表示正常使用
private int flag;

public int getFlag() ...{
return flag;
}
public void setFlag(int flag) ...{
this.flag = flag;
}
public double getMoney() ...{
return money;
}
public void setMoney(double money) ...{
this.money = money;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getPhonenb() ...{
return phonenb;
}
public void setPhonenb(String phonenb) ...{
this.phonenb = phonenb;
}
}
cardPO.java 充值卡信息:
package po;

public class cardPO ...{
//充值卡密码
private String password;
//充值卡金额
private double money;
public double getMoney() ...{
return money;
}
public void setMoney(double money) ...{
this.money = money;
}
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
}
实现两个接口:
业务层:ServiceImp.java
package business;
import dao.IMobileDAO;
import po.cardPO;
import po.phonePO;

public class ServiceImp implements IService ...{
private IMobileDAO mobile;
public boolean addMoney(phonePO phone, cardPO card) ...{
boolean isok=false;
if(mobile.isPhoneExist(phone) && mobile.isCardExist(card))...{
phone.setMoney(getMobile().queryBalance(phone)+getMobile().queryCardMoney(card));
isok=getMobile().addMoney(phone);
}
return isok;
}

public boolean delAccount(phonePO phone) ...{
boolean isok=false;
if(mobile.isPhoneExist(phone))...{
isok=getMobile().delAccount(phone);
}
return isok;
}

public double queryBalance(phonePO phone) ...{
double money=0;
if(mobile.isPhoneExist(phone))...{
money=getMobile().queryBalance(phone);
}
return money;
}

public IMobileDAO getMobile() ...{
return mobile;
}

public void setMobile(IMobileDAO mobile) ...{
this.mobile = mobile;
}

public boolean isUser(phonePO phone) ...{
return getMobile().isPhoneExist(phone);
}
}
dao层:MobileDAO.java
package dao;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import po.cardPO;
import po.phonePO;
import org.springframework.jdbc.core.JdbcTemplate;

public class MobileDAO implements IMobileDAO ...{
private DataSource data;
private JdbcTemplate jdbcTemplate=null;

public boolean addMoney(phonePO phone) ...{
boolean isok=false;
try...{
int i=jdbcTemplate.update("update phone set money="+phone.getMoney()+" where phonenb='"+phone.getPhonenb()+"'");
if(i!=0)...{
isok=true;
}
}catch(Exception e)...{
e.printStackTrace();
}
return isok;
}

public boolean delAccount(phonePO phone) ...{
boolean isok=false;
try...{
int i=jdbcTemplate.update("update phone set flag=0 where phonenb='"+phone.getPhonenb()+"'");
if(i!=0)...{
isok=true;
}
}catch(Exception e)...{
e.printStackTrace();
}
return isok;
}

public boolean isCardExist(cardPO card) ...{
boolean isok=false;
try...{
List list=(List)jdbcTemplate.queryForList("select * from card where password='"+card.getPassword()+"'");
Iterator it=list.iterator();
//System.out.println("isCardExist()");
if(it.hasNext())...{
isok=true;
}
}catch(Exception e)...{
e.printStackTrace();
}
return isok;
}

public boolean isPhoneExist(phonePO phone) ...{
boolean isok=false;
try...{
List list=(List)jdbcTemplate.queryForList("select * from phone where phonenb='"+phone.getPhonenb()+"' and flag=1 and password="+phone.getPassword()+"");
//System.out.println(phone.getPhonenb());
Iterator it=list.iterator();

if(it.hasNext())...{
isok=true;
//System.out.println("isPhoneExist");
}
}catch(Exception e)...{
e.printStackTrace();
}
return isok;
}

public double queryBalance(phonePO phone) ...{
double balance = 0;
try...{
List list=jdbcTemplate.queryForList("select * from phone where phonenb='"+phone.getPhonenb()+"'");
Iterator it=list.iterator();
if(it.hasNext())...{
Map map=(Map)it.next();
String Balance=map.get("money").toString();
balance=Double.parseDouble(Balance);
}
}catch(Exception e)...{
e.printStackTrace();
}
return balance;
}

public double queryCardMoney(cardPO card) ...{
double money = 0;
try...{
List list=jdbcTemplate.queryForList("select * from card where password='"+card.getPassword()+"'");
Iterator it=list.iterator();
if(it.hasNext())...{
Map map=(Map)it.next();
String Money=map.get("money").toString();
money=Double.parseDouble(Money);
}
if(money!=0)...{
jdbcTemplate.update("delete from card where password='"+card.getPassword()+"'");
}
}catch(Exception e)...{
e.printStackTrace();
}
return money;
}

public DataSource getData() ...{
return data;
}

public void setData(DataSource data) ...{
jdbcTemplate = new JdbcTemplate(data);
}
}表现层:
//充值
package view;
import po.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import business.IService;

public class addMoney ...{
public static void main(String[] args)...{
ApplicationContext context =
new FileSystemXmlApplicationContext("src/applicationContext.xml");
IService service=(IService) context.getBean("myservice");
phonePO phone=new phonePO();
cardPO card=new cardPO();
phone.setPhonenb("12345678912");//输入手机号
phone.setPassword("123456");//输入手机密码
card.setPassword("123456789");//输入充值卡密码
boolean isok=service.addMoney(phone, card);
if(isok)...{
System.out.println("充值成功!");
}else...{
System.out.println("您输入的手机号不存在或充值卡密码错误!");
}
}
}销户:
package view;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import po.phonePO;
import business.IService;

public class delAcount ...{
public static void main(String[] args)...{
ApplicationContext context =
new FileSystemXmlApplicationContext("src/applicationContext.xml");
IService service=(IService) context.getBean("myservice");
phonePO phone=new phonePO();
phone.setPhonenb("12345678910");//输入手机号
phone.setPassword("123456");//输入手机密码
boolean isok=service.delAccount(phone);
if(isok)...{
System.out.println("注销成功!");
}else...{
System.out.println("您输入的手机号不存在或密码误!");
}
}
}//查话费:
package view;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import po.phonePO;
import business.IService;

public class queryMoney ...{
public static void main(String[] args)...{
ApplicationContext context =
new FileSystemXmlApplicationContext("src/applicationContext.xml");
IService service=(IService) context.getBean("myservice");
phonePO phone=new phonePO();
phone.setPhonenb("13936462854");//输入手机号
phone.setPassword("619347");//输入手机密码
double isok=service.queryBalance(phone);
if(isok!=0)...{
System.out.println("您的余额为:"+isok);
}else...{
System.out.println("您输入的手机号不存在或密码误!");
}
}
}配置文件:(会在下文中做详细解释)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mydata"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:oracle</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>tiger</value>
</property>
</bean>
<bean id="mydao"
class="dao.MobileDAO">
<property name="data">
<ref bean="mydata"/>
</property>
</bean>
<bean id="myservice"
class="business.ServiceImp">
<property name="mobile">
<ref bean="mydao"/>
</property>
</bean>

</beans>
623

被折叠的 条评论
为什么被折叠?



