Spring 会员账号管理例子,采用注解方式,研究有参的构造函数使用

 首先是配置注解关链
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <!-- 扫描的基本包路径 -->
	<context:component-scan base-package="指向你存放类的路径"/>
</beans>

一:账户的model层模型

import java.util.Date;

//account 帐户
public class Account {
    private long id;
    private String ownerName; //帐户所有人
    private double balance; //使什么保持平衡
    private Date accessTime;
    private boolean locked;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getOwnerName() {
        return ownerName;
    }
    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public Date getAccessTime() {
        return accessTime;
    }
    public void setAccessTime(Date accessTime) {
        this.accessTime = accessTime;
    }
    public boolean isLocked() {
        return locked;
    }
    public void setLocked(boolean locked) {
        this.locked = locked;
    }
    
    //getters & setters...
}

二:ado层
1)接口

import java.util.List;


public interface AccountDao {
    public void insert(Account account);
    /**
     * 单个更改帐号
     * **/
    public void update(Account account);
    /**
     * 批量更改帐号
     * **/
    public void update(List<Account> accounts);
    public void delete(long accountId);
    public Account find(long accountId);
    public List<Account> find(List<Long> accountIds);
    public List<Account> find(String ownerName);
    public List<Account> find(boolean locked);
}


2)接口的实现

package com.wiley.beginningspring.ch2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoInMemoryImpl implements AccountDao {
	
	private Map<Long,Account> accountsMap = new HashMap<>();
	
	{
		Account account1 = new Account();
		account1.setId(1L);
		account1.setOwnerName("John");
		account1.setBalance(10.0);
		
		Account account2 = new Account();
		account2.setId(2L);
		account2.setOwnerName("Mary");
		account2.setBalance(20.0);
		
		accountsMap.put(account1.getId(), account1);
		accountsMap.put(account2.getId(), account2);
		
	}

	@Override
	public void insert(Account account) {
		accountsMap.put(account.getId(), account);
	}

	@Override
	public void update(Account account) {
		accountsMap.put(account.getId(), account);
	}

	@Override
	public void update(List<Account> accounts) {
		for(Account account:accounts) {
			update(account);
		}
	}

	@Override
	public void delete(long accountId) {
		accountsMap.remove(accountId);
	}

	@Override
	public Account find(long accountId) {
		return accountsMap.get(accountId);
	}

	@Override
	public List<Account> find(List<Long> accountIds) {
		List<Account> accounts = new ArrayList<>();
		for(Long id:accountIds) {
			accounts.add(accountsMap.get(id));
		}
		return accounts;
	}

	@Override
	public List<Account> find(String ownerName) {
		List<Account> accounts = new ArrayList<>();
		for(Account account:accountsMap.values()) {
			if(ownerName.equals(account.getOwnerName())) {
				accounts.add(account);
			}
		}
		return accounts;
	}

	@Override
	public List<Account> find(boolean locked) {
		List<Account> accounts = new ArrayList<>();
		for(Account account:accountsMap.values()) {
			if(locked == account.isLocked()) {
				accounts.add(account);
			}
		}
		return accounts;
	}

}

三: service层
1)接口

public interface AccountService {
    public void transferMoney(long sourceAccountId, long targetAccountId, double amount);
    public void depositMoney(long accountId, double amount) throws Exception;
    public Account getAccount(long accountId);
}

2)接口实现


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {
	private AccountDao accountDao;

	@Autowired
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	public void transferMoney(long sourceAccountId, long targetAccountId, double amount) {
		Account sourceAccount = accountDao.find(sourceAccountId);
		Account targetAccount = accountDao.find(targetAccountId);
		sourceAccount.setBalance(sourceAccount.getBalance() - amount);
		targetAccount.setBalance(targetAccount.getBalance() + amount);
		accountDao.update(sourceAccount);
		accountDao.update(targetAccount);
	}

	@Override
	public void depositMoney(long accountId, double amount) throws Exception {
		Account account = accountDao.find(accountId);
		account.setBalance(account.getBalance() + amount);
		accountDao.update(account);
	}

	@Override
	public Account getAccount(long accountId) {
		return accountDao.find(accountId);
	}	
}


三:调用

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/wiley/beginningspring/ch2/ch2-beans.xml");
		AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);

		System.out.println("Before money transfer");
		System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
		System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());		
		
		accountService.transferMoney(1, 2, 5.0);
		
		System.out.println("After money transfer");
		System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
		System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值