Dao层和Service层

Dao 层:数据访问层;

Service层:业务层;

举个实例:现在要做一个银行操作系统:

Dao模型需要先提供一个Dao接口;
然后再提供一个Dao接口的实现类;
再编写一个Dao工厂,Service通过工厂来获取Dao实现;

package com.why.dao;

import java.util.List;

import com.why.domain.Account;
/**
 * 账户Dao,专门对账户做增删改查操作的
 * @author TF
 *
 */
public interface AccountDao {
	
	/**
	 * 完成对银行账户数据的持久化操作
	 * @param accounts 账户对象的集合
	 * @throws Exception
	 */
	void write(List<Account> accounts) throws Exception;
	
	/**
	 * 读取所有银行账户对象的集合
	 * @return
	 * @throws Exception
	 */
	List<Account> read() throws Exception;
	
	/**
	 * 根据账户读取账户对象
	 * @param accountNo 银行账户
	 * @return 返回满足账号的账户对象
	 * @throws Exception
	 */
	Account read(String accountNo) throws Exception;
	
	/**
	 * 根据登录时管理员的账户和密码的合法性
	 * @param accountNo 管理员账户
	 * @param accountPwd 管理员密码
	 * @return 管理员对象,如果账户密码不合法则返回null
	 * 
	 * @throws Exception
	 */
	Account read(String accountNo,String accountPwd) throws Exception;

}
package com.why.dao.bean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.ResourceBundle;

import com.why.dao.AccountDao;
import com.why.domain.Account;
import com.why.globals.Globals;

public class AccountDaoBean implements AccountDao {

	@Override
	public void write(List<Account> accounts) throws Exception {
		ObjectOutputStream objectOutputStream=null;
		try{
			objectOutputStream=new ObjectOutputStream(new FileOutputStream(Globals.ACCOUNT_FILE));
			objectOutputStream.writeObject(accounts);
			objectOutputStream.flush();
		}finally{
			if(objectOutputStream!=null){
				objectOutputStream.close();
				objectOutputStream=null;
			}
		}

	}

	
	@Override
	public List<Account> read() throws Exception {
		ObjectInputStream objectInputStream=null;
		
		try{
			File file=new File(Globals.ACCOUNT_FILE);
			if(!file.exists()){
				throw new Exception("账户文件不存在");
				//return null;
			}
			objectInputStream=new ObjectInputStream(new FileInputStream(file));
			
			List<Account> accounts=(List<Account>) objectInputStream.readObject();
			
			return accounts;
		}finally{
			if(objectInputStream!=null){
				objectInputStream.close();
				objectInputStream=null;
			}
		}
		
	}

	@Override
	public Account read(String accountNo) throws Exception {
		List<Account> accounts=this.read();
		if(accounts!=null){
			for (Account account : accounts) {
				if(account.getAccountNo().equals(accountNo)){
					return account;
				}
			}
			throw new Exception("账户为"+accountNo+"不存在");
		}else{
			throw new Exception("银行账户文件不存在");
		}
		
	}

	@Override
	public Account read(String accountNo, String accountPwd) throws Exception {
		ResourceBundle resourceBundle=ResourceBundle.getBundle("AdminConfig");
		String name=resourceBundle.getString("userName");
		String pwd=resourceBundle.getString("pwd");
		
		if(!(accountNo.equals(name) && accountPwd.equals(pwd))){
			throw new Exception("账户或密码错误");
			
		}
		
		Account account=new Account(name,pwd,0,null);
		
		
		return account;
	}

}
package com.why.dao.factory;


import com.why.dao.AccountDao;
import com.why.dao.bean.AccountDaoBean;


/**
 * 
 * 自定义Dao工厂类
 * 工厂类的好处:
 * 	1、将对象的实例化进行了统一管理
 * 	2、由于方法返回的是接口类型,因为对于调用者而言,
 * 	只识别接口类型的对象即可,不需要知道具体实现是什么
 * 
 * @author TF
 *
 */
public class DaoFactory {
	
	/**
	 * 方法返回的是接口类型,但是实现类中的实例化了接口的实现类
	 * @return
	 */
	public static AccountDao getAccountDao(){
		return new AccountDaoBean();
	}
	


}

Service是业务层,也是先提供一个Service接口;

然后再提供一个Service接口的实现类;

再编写一个Service工厂,方便通过工厂来获取Service实现;

package com.why.service;

import java.util.List;

import com.why.domain.Account;

public interface AccountService {

	/**
	 * 普通银行账户的登录
	 * @param accountNo 登录的账号
	 * @param accountPwd 登录的密码
	 * @return 登录成功返回true,登陆失败返回false
	 */
	boolean login(String accountNo,String accountPwd) throws Exception;
	
	/**
	 * 管理员登录
	 * @param accountNo 登录的账号
	 * @param accountPwd 登录的密码
	 * @return 登录成功返回true,登陆失败返回false
	 * @throws Exception
	 */
	boolean adminLogin(String accountNo,String accountPwd) throws Exception;
	
	/**
	 * 开户功能
	 * @param account 开户时对应的账户对象
	 * @return 开户成功返回true
	 * @throws Exception
	 */
	boolean open(Account account) throws Exception;
	
	
	/**
	 * 查询所有账户
	 * @return 返回所有账户的集合
	 * @throws Exception
	 */
	List<Account> query() throws Exception;
	
	
	/**
	 * 根据银行账号查询银行账户
	 * @param accountNo 要查询的银行账号
	 * @return 返回要查询的银行账户
	 * @throws Exception
	 */
	Account query(String accountNo) throws Exception;
	
	
	/**
	 * 存款
	 * @param accountNo 账号
	 * @param balance 存款的金额
	 * @return 存款是否成功
	 * @throws Exception
	 */
	boolean deposit(double balance) throws Exception;
	
	
	/**
	 * 取款
	 * @param accountNo 账号
	 * @param balance 取款金额
	 * @return 取款是否成功
	 * @throws Exception
	 */
	boolean withdraw(double balance) throws Exception;
	
	/**
	 * 转账
	 * @param accountNo 对方账号
	 * @param balance 转账金额
	 * @return 转账是否成功
	 * @throws Exception
	 */
	boolean transfer(String accountNo,double balance) throws Exception;
	
	
	/**
	 * 修改密码
	 * @param newPwd 新密码
	 * @return 修改密码是否成功
	 * @throws Exception
	 */
	boolean changePwd(String newPwd) throws Exception;
	
	
	/**
	 * 冻结账户
	 * @param accountNo 账号
	 * @return 冻结账户是否成功
	 * @throws Exception
	 */
	boolean freeze(String accountNo) throws Exception;
	
	/**
	 * 解冻账户
	 * @param accountNo 账号
	 * @return 冻结账户是否成功
	 * @throws Exception
	 */
	boolean unfreeze(String accountNo) throws Exception;
	
	/**
	 * 挂失指定账户
	 * @param accountNo 需要挂失账户的账号
	 * @return 挂失是否成功
	 * @throws Exception
	 */
	boolean lost(String accountNo) throws Exception;
	
	/**
	 * 解挂指定账户
	 * @param accountNo 需要解挂的账户的账号
	 * @return 解挂是否成功
	 * @throws Exception
	 */
	boolean unlost(String accountNo) throws Exception;
	
	/**
	 * 根据账号销毁指定账户对象
	 * @param accountNo 需要销户的账号
	 * @return 销户是否成功
	 * @throws Exception
	 */
	boolean destroy(String accountNo) throws Exception;
}
package com.why.service.bean;

import java.util.List;

import com.why.dao.AccountDao;
import com.why.dao.factory.DaoFactory;
import com.why.domain.Account;
import com.why.domain.AccountStatus;
import com.why.service.AccountService;

public class AccountServiceBean implements AccountService {

	private AccountDao accountDao;
	
	private String currentAccountNo;
	
	public String getCurrentAccountNo() {
		return currentAccountNo;
	}
	public AccountServiceBean() {
		accountDao=DaoFactory.getAccountDao();
	}

	@Override
	public boolean login(String accountNo, String accountPwd) throws Exception {
		List<Account> accounts=accountDao.read();
		boolean isLogin=false;
		for (Account account : accounts) {
			
			if(account.getAccountNo().equals(accountNo) && account.getAccountPwd().equals(accountPwd)){
				
				this.currentAccountNo=accountNo;
				isLogin=true;
			}
		}
		return isLogin;
	}

	@Override
	public boolean adminLogin(String accountNo, String accountPwd) throws Exception {
		Account account=accountDao.read(accountNo, accountPwd);
		boolean isLogin=false;
		
		if(account.getAccountNo().equals(accountNo) && account.getAccountPwd().equals(accountPwd)){
				
			this.currentAccountNo=accountNo;
			isLogin=true;
		}
		
		return isLogin;
	}

	@Override
	public boolean open(Account account) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account item : accounts) {
			if(account.getAccountNo().equals(item.getAccountNo())){
				System.out.println("账户已存在");
				return false;
			}
		}
		accounts.add(account);
		accountDao.write(accounts);
		return true;
	}

	@Override
	public List<Account> query() throws Exception {
		
		return accountDao.read();
	}

	@Override
	public Account query(String accountNo) throws Exception {
		
		return accountDao.read(accountNo);
	}

	@Override
	public boolean deposit(double balance) throws Exception {
		
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(this.currentAccountNo)){
				
				account.setBalance(account.getBalance()+balance);
				accountDao.write(accounts);
				return true;
			}
			
		}
		return false;
	}

	@Override
	public boolean withdraw(double balance) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(this.currentAccountNo)){
				double result=account.getBalance()-balance;
				if(result<0){
					throw new Exception("余额不足,取款失败");
				}
				account.setBalance(result);
				accountDao.write(accounts);
				return true;
			}
			
		}
		
		return false;
	}

	@Override
	public boolean transfer(String accountNo, double balance) throws Exception {
		List<Account> accounts=accountDao.read();
		boolean isOne=false;
		boolean isTwo=false;
		for (Account account : accounts) {
			if(account.getAccountNo().equals(this.currentAccountNo)){
				double result=account.getBalance()-balance;
				if(result<0){
					throw new Exception("余额不足,转账失败");
				}
				account.setBalance(result);
				isOne=true;
				
			}
			
			if(account.getAccountNo().equals(accountNo)){
				account.setBalance(account.getBalance()+balance);
				isTwo=true;
			}
		}
		
		if(isOne && isTwo){
			accountDao.write(accounts);
			return true;
		}
		return false;
	}

	@Override
	public boolean changePwd(String newPwd) throws Exception {
		List<Account> accounts=accountDao.read();
		
		for (Account account : accounts) {
			if(account.getAccountNo().equals(this.currentAccountNo)){
				account.setAccountPwd(newPwd);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean freeze(String accountNo) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(accountNo)){
				account.setAccountStatus(AccountStatus.FREEZE);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean unfreeze(String accountNo) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(accountNo)){
				account.setAccountStatus(AccountStatus.NORMAL);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean lost(String accountNo) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(accountNo)){
				account.setAccountStatus(AccountStatus.LOST);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean unlost(String accountNo) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(accountNo)){
				account.setAccountStatus(AccountStatus.NORMAL);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean destroy(String accountNo) throws Exception {
		List<Account> accounts=accountDao.read();
		for (Account account : accounts) {
			if(account.getAccountNo().equals(accountNo)){
				accounts.remove(account);
				accountDao.write(accounts);
				return true;
			}
		}
		return false;
	}

}
package com.why.service.factory;

import com.why.service.AccountService;
import com.why.service.bean.AccountServiceBean;

public class ServiceFactory {
	public static AccountService getAccountService(){
		return new AccountServiceBean();
	}
}





  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DAO层和Service层是软件开发中常见的两个层次。其中,DAO层是数据访问层,Service层是业务逻辑层。下面详细解释两者的区别和作用。 DAO层(数据访问层) DAO全称为Data Access Object,是数据访问对象。它是负责数据访问和持久存储的一层,主要用于和数据库进行交互。在DAO层中,开发人员通常会定义一些接口,这些接口定义了与数据库相关的操作方法,比如查询、增、删、改等。接着,开发人员会根据需要实现这些接口,以便具体的业务逻辑层可以调用这些方法来操作数据库。 DAO层的作用是将数据访问操作和业务逻辑分离开来,使得业务逻辑层无需关心具体的数据库操作。这样,当数据库发生变化时,业务逻辑层不需要做任何改动,只需要修改DAO层的实现即可。 Service层(业务逻辑层) Service层是业务逻辑层,它是连接DAO层和Controller层的中间层。在Service层中,开发人员会定义一些接口,这些接口定义了业务逻辑相关的操作方法,比如用户注册、登录、查询等。接着,开发人员会根据需要实现这些接口,以便具体的Controller层可以调用这些方法来进行业务逻辑操作。 Service层的主要作用是处理业务逻辑,将DAO层提供的数据操作方法组合成具体的业务逻辑。同时,在Service层中也可以进行一些数据校验、数据转换等操作。Service层可以看做是一个业务逻辑的集合,将多个DAO操作组合成一个完整的业务流程。因此,Service层的设计需要考虑业务逻辑的复杂性和可重用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值