dao层结束之后,来写service层,就是业务逻辑层,这一层也采用接口加impl的方式实现。比如atm的业务层:
package org.atm.service;
import java.util.*;
import org.atm.po.*;
public interface AtmService
{
/**
*返回卡的信息
*/
public Card getCard(String cardNumber);
/**
*显示余额
*/
public double showBalance(String cardNumber);
/**修改密码
*/
public void updateCardPass(String cardNumber,String newPassword);
/**取款
*/
public String getMoney(String cardNumber,double money,int atm_id);
/**存款
*/
public void putMoney(String cardNumber,double money,int atm_id);
/**转账
*/
public String transferMoney(String cardNumber,String tocardNumber,double money,int atm_id);
/**打印凭条(即打印最近一次CardLog)
*/
public CardLog printTip(String cardNumber);
/**查看存取记录
*/
public Set<CardLog> showCardLogs(String cardNumber);
}
package org.atm.service.impl;
import java.util.*;
import org.atm.dao.*;
import org.atm.service.*;
import org.atm.po.*;
import org.atm.util.*;
public class AtmServiceImpl implements AtmService
{
private AtmDao atmDao;
private CustomerDao customerDao;
private CardDao cardDao;
private CardLogDao cardlogDao;
private CardTypeDao cardtypeDao;
private BankDao bankDao;
private BankEmployeeDao bankemployeeDao;
private AtmLogDao atmlogDao;
public BankEmployeeDao getBankemployeeDao() {
return bankemployeeDao;
}
public void setBankemployeeDao(BankEmployeeDao bankemployeeDao) {
this.bankemployeeDao = bankemployeeDao;
}
public AtmDao getAtmDao() {
return atmDao;
}
public void setAtmDao(AtmDao atmDao) {
this.atmDao = atmDao;
}
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public CardDao getCardDao() {
return cardDao;
}
public void setCardDao(CardDao cardDao) {
this.cardDao = cardDao;
}
public CardLogDao getCardlogDao() {
return cardlogDao;
}
public void setCardlogDao(CardLogDao cardlogDao) {
this.cardlogDao = cardlogDao;
}
public CardTypeDao getCardtypeDao() {
return cardtypeDao;
}
public void setCardtypeDao(CardTypeDao cardtypeDao) {
this.cardtypeDao = cardtypeDao;
}
public BankDao getBankDao() {
return bankDao;
}
public void setBankDao(BankDao bankDao) {
this.bankDao = bankDao;
}
public AtmLogDao getAtmlogDao()
{
return atmlogDao;
}
public void setAtmlogDao(AtmLogDao atmlogDao)
{
this.atmlogDao=atmlogDao;
}
/**
*返回卡的信息
*/
public Card getCard(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0);
}
/**
*显示余额
*/
public double showBalance(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0).getBalance();
}
/**修改密码
*/
public void updateCardPass(String cardNumber,String newPassword)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
card.setPassword(newPassword);
cardDao.update(card);
System.out.println("密码更新成功");
}
/**取款
*/
public String getMoney(String cardNumber,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
double balance=card.getBalance();
if(balance>=money)
{
Atm atm=atmDao.get(atm_id);
if((atm.getMoney())>=money)
{
card.setBalance(balance-money);
cardDao.update(card);
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("取款"+money);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
AtmLog atmlog=new AtmLog();
atmlog.setId(1);
atmlog.setLogmessage("-"+money);
atmlog.setAtm(atmDao.get(atm_id));
atmlog.setTime(DateToString.getString(new Date()));
atmlogDao.save(atmlog);
atm.setMoney(atm.getMoney()-money);
atmDao.save(atm);
return "正在出钞";
}
else
return "ATM余额不足";
}
else
return "账户余额不足";
}
/**存款
*/
public void putMoney(String cardNumber,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
card.setBalance(card.getBalance()+money);
cardDao.update(card);
//卡的记录
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("存款"+money);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
//ATM的记录
AtmLog atmlog=new AtmLog();
atmlog.setId(1);
atmlog.setLogmessage("+"+money);
atmlog.setAtm(atmDao.get(atm_id));
atmlog.setTime(DateToString.getString(new Date()));
atmlogDao.save(atmlog);
Atm atm=atmDao.get(atm_id);
atm.setMoney(atm.getMoney()+money);
atmDao.save(atm);
}
/**转账
*/
public String transferMoney(String cardNumber,String tocardNumber ,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
double balance=card.getBalance();
if(balance>=money)
{
card.setBalance(card.getBalance()-money);
cardDao.update(card);
Card card2=cardDao.findByCardNumber(tocardNumber).get(0);
card2.setBalance(card2.getBalance()+money);
cardDao.update(card2);
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("转"+money+"元到"+tocardNumber);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
return "正在转账";
}
else
return "账户余额不足";
}
/**打印凭条(即打印最近一次CardLog)
*/
public CardLog printTip(String cardNumber)
{
return cardlogDao.findLastLog(getCard(cardNumber));
}
/**查看存取记录
*/
public Set<CardLog> showCardLogs(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0).getCardlogs();
}
}
可以看到,有了dao的基础业务层十分简单。这里要想到一个问题,就是业务逻辑层的实现类的属性要有全部的dao对象,如何创建这些对象,spring出场了,看看配置文件:
<bean id="serviceTemplate" abstract="true" lazy-init="true">
<property name="customerDao" ref="customerDao"/>
<property name="cardDao" ref="cardDao"/>
<property name="cardtypeDao" ref="cardtypeDao"/>
<property name="cardlogDao" ref="cardlogDao"/>
<property name="bankDao" ref="bankDao"/>
<property name="atmDao" ref="atmDao"/>
<property name="bankemployeeDao" ref="bankemployeeDao"/>
<property name="atmlogDao" ref="atmlogDao"/>
</bean>
<bean id="atmService" class="org.atm.service.impl.AtmServiceImpl" parent="serviceTemplate"/>
<bean id="bankEmployeeService" class="org.atm.service.impl.BankEmployeeServiceImpl" parent="serviceTemplate"/>
其中有两个业务逻辑atmService和bankEmployeeService,他们都具有所有dao层的对象作为属性,即使用时有spring框架依赖注入。
但是还要注意,我们希望业务层的每个方法具有事务性,即每个方法中对数据库的操作同时成功或者失败,这时我们要考虑事务管理,Spring框架提供了食物是管理,配置文件如下:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="leePointcut" expression="bean(atmService)||bean(bankEmployeeService)"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="leePointcut"/>
</aop:config>
自此,业务逻辑层完毕。
package org.atm.service;
import java.util.*;
import org.atm.po.*;
public interface AtmService
{
/**
*返回卡的信息
*/
public Card getCard(String cardNumber);
/**
*显示余额
*/
public double showBalance(String cardNumber);
/**修改密码
*/
public void updateCardPass(String cardNumber,String newPassword);
/**取款
*/
public String getMoney(String cardNumber,double money,int atm_id);
/**存款
*/
public void putMoney(String cardNumber,double money,int atm_id);
/**转账
*/
public String transferMoney(String cardNumber,String tocardNumber,double money,int atm_id);
/**打印凭条(即打印最近一次CardLog)
*/
public CardLog printTip(String cardNumber);
/**查看存取记录
*/
public Set<CardLog> showCardLogs(String cardNumber);
}
package org.atm.service.impl;
import java.util.*;
import org.atm.dao.*;
import org.atm.service.*;
import org.atm.po.*;
import org.atm.util.*;
public class AtmServiceImpl implements AtmService
{
private AtmDao atmDao;
private CustomerDao customerDao;
private CardDao cardDao;
private CardLogDao cardlogDao;
private CardTypeDao cardtypeDao;
private BankDao bankDao;
private BankEmployeeDao bankemployeeDao;
private AtmLogDao atmlogDao;
public BankEmployeeDao getBankemployeeDao() {
return bankemployeeDao;
}
public void setBankemployeeDao(BankEmployeeDao bankemployeeDao) {
this.bankemployeeDao = bankemployeeDao;
}
public AtmDao getAtmDao() {
return atmDao;
}
public void setAtmDao(AtmDao atmDao) {
this.atmDao = atmDao;
}
public CustomerDao getCustomerDao() {
return customerDao;
}
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public CardDao getCardDao() {
return cardDao;
}
public void setCardDao(CardDao cardDao) {
this.cardDao = cardDao;
}
public CardLogDao getCardlogDao() {
return cardlogDao;
}
public void setCardlogDao(CardLogDao cardlogDao) {
this.cardlogDao = cardlogDao;
}
public CardTypeDao getCardtypeDao() {
return cardtypeDao;
}
public void setCardtypeDao(CardTypeDao cardtypeDao) {
this.cardtypeDao = cardtypeDao;
}
public BankDao getBankDao() {
return bankDao;
}
public void setBankDao(BankDao bankDao) {
this.bankDao = bankDao;
}
public AtmLogDao getAtmlogDao()
{
return atmlogDao;
}
public void setAtmlogDao(AtmLogDao atmlogDao)
{
this.atmlogDao=atmlogDao;
}
/**
*返回卡的信息
*/
public Card getCard(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0);
}
/**
*显示余额
*/
public double showBalance(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0).getBalance();
}
/**修改密码
*/
public void updateCardPass(String cardNumber,String newPassword)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
card.setPassword(newPassword);
cardDao.update(card);
System.out.println("密码更新成功");
}
/**取款
*/
public String getMoney(String cardNumber,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
double balance=card.getBalance();
if(balance>=money)
{
Atm atm=atmDao.get(atm_id);
if((atm.getMoney())>=money)
{
card.setBalance(balance-money);
cardDao.update(card);
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("取款"+money);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
AtmLog atmlog=new AtmLog();
atmlog.setId(1);
atmlog.setLogmessage("-"+money);
atmlog.setAtm(atmDao.get(atm_id));
atmlog.setTime(DateToString.getString(new Date()));
atmlogDao.save(atmlog);
atm.setMoney(atm.getMoney()-money);
atmDao.save(atm);
return "正在出钞";
}
else
return "ATM余额不足";
}
else
return "账户余额不足";
}
/**存款
*/
public void putMoney(String cardNumber,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
card.setBalance(card.getBalance()+money);
cardDao.update(card);
//卡的记录
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("存款"+money);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
//ATM的记录
AtmLog atmlog=new AtmLog();
atmlog.setId(1);
atmlog.setLogmessage("+"+money);
atmlog.setAtm(atmDao.get(atm_id));
atmlog.setTime(DateToString.getString(new Date()));
atmlogDao.save(atmlog);
Atm atm=atmDao.get(atm_id);
atm.setMoney(atm.getMoney()+money);
atmDao.save(atm);
}
/**转账
*/
public String transferMoney(String cardNumber,String tocardNumber ,double money,int atm_id)
{
Card card=cardDao.findByCardNumber(cardNumber).get(0);
double balance=card.getBalance();
if(balance>=money)
{
card.setBalance(card.getBalance()-money);
cardDao.update(card);
Card card2=cardDao.findByCardNumber(tocardNumber).get(0);
card2.setBalance(card2.getBalance()+money);
cardDao.update(card2);
CardLog cardlog=new CardLog();
cardlog.setId(1);
cardlog.setLogmessage("转"+money+"元到"+tocardNumber);
cardlog.setCard(card);
cardlog.setTime(DateToString.getString(new Date()));
cardlog.setAtm(atmDao.get(atm_id));
cardlogDao.save(cardlog);
return "正在转账";
}
else
return "账户余额不足";
}
/**打印凭条(即打印最近一次CardLog)
*/
public CardLog printTip(String cardNumber)
{
return cardlogDao.findLastLog(getCard(cardNumber));
}
/**查看存取记录
*/
public Set<CardLog> showCardLogs(String cardNumber)
{
return cardDao.findByCardNumber(cardNumber).get(0).getCardlogs();
}
}
可以看到,有了dao的基础业务层十分简单。这里要想到一个问题,就是业务逻辑层的实现类的属性要有全部的dao对象,如何创建这些对象,spring出场了,看看配置文件:
<bean id="serviceTemplate" abstract="true" lazy-init="true">
<property name="customerDao" ref="customerDao"/>
<property name="cardDao" ref="cardDao"/>
<property name="cardtypeDao" ref="cardtypeDao"/>
<property name="cardlogDao" ref="cardlogDao"/>
<property name="bankDao" ref="bankDao"/>
<property name="atmDao" ref="atmDao"/>
<property name="bankemployeeDao" ref="bankemployeeDao"/>
<property name="atmlogDao" ref="atmlogDao"/>
</bean>
<bean id="atmService" class="org.atm.service.impl.AtmServiceImpl" parent="serviceTemplate"/>
<bean id="bankEmployeeService" class="org.atm.service.impl.BankEmployeeServiceImpl" parent="serviceTemplate"/>
其中有两个业务逻辑atmService和bankEmployeeService,他们都具有所有dao层的对象作为属性,即使用时有spring框架依赖注入。
但是还要注意,我们希望业务层的每个方法具有事务性,即每个方法中对数据库的操作同时成功或者失败,这时我们要考虑事务管理,Spring框架提供了食物是管理,配置文件如下:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="leePointcut" expression="bean(atmService)||bean(bankEmployeeService)"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="leePointcut"/>
</aop:config>
自此,业务逻辑层完毕。