类总体介绍
假设我们现在有一个基于 Spring 的应用程序,除了 MVC 层,还包括业务层和数据访问层,业务层有一个类 AccountService,负责处理账号类的业务,其依赖于数据访问层 AccountDao 类,此类提供了基于 Spring Jdbc Template 实现的数据库访问方法,AccountService 和 AccountDao 以及他们之间的依赖关系都是通过 Spring 配置文件进行管理的。
现在我们要对 AccountService 类进行测试,在不使用 Spring 测试方法之前,我们需要这样做:
清单 1. Account.Java
此类代表账号的基本信息,提供 getter 和 setter 方法。
- package domain;
-
- public class Account {
- public static final String SEX_MALE = "male";
- public static final String SEX_FEMALE = "female";
-
- private int id;
- private String name;
- private int age;
- private String sex;
- public String toString() {
- return String.format("Account[id=%d,name=%s,age:%d,sex:%s]",id,name,age,sex);
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public static Account getAccount(int id,String name,int age,String sex) {
- Account acct = new Account();
- acct.setId(id);
- acct.setName(name);
- acct.setAge(age);
- acct.setSex(sex);
- return acct;
- }
- }
|
注意上面的 Account 类有一个 toString() 方法和一个静态的 getAccount 方法,getAccount 方法用于快速获取 Account 测试对象。
清单 2. AccountDao.Java
这个 DAO 我们这里为了简单起见,采用 Spring Jdbc Template 来实现。
- package DAO;
-
- import Java.sql.ResultSet;
- import Java.sql.SQLException;
- import Java.util.HashMap;
- import Java.util.List;
- import Java.util.Map;
-
- import org.Springframework.context.ApplicationContext;
- import org.Springframework.context.support.ClassPathXmlApplicationContext;
- import org.Springframework.jdbc.core.RowMapper;
- import org.Springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
- import org.Springframework.jdbc.core.simple.ParameterizedRowMapper;
-
- import domain.Account;
-
- public class AccountDao extends NamedParameterJdbcDaoSupport {
- public void saveAccount(Account account) {
- String sql = "insert into tbl_account(id,name,age,sex) " +
- "values(:id,:name,:age,:sex)";
- Map paramMap = new HashMap();
- paramMap.put("id", account.getId());
- paramMap.put("name", account.getName());
- paramMap.put("age", account.getAge());
- paramMap.put("sex",account.getSex());
- getNamedParameterJdbcTemplate().update(sql, paramMap);
- }
-
- public Account getAccountById(int id) {
- String sql = "select id,name,age,sex from tbl_account where id=:id";
- Map paramMap = new HashMap();
- paramMap.put("id", id);
- List<Account> matches = getNamedParameterJdbcTemplate().query(sql,
- paramMap,new ParameterizedRowMapper<Account>() {
- @Override
- public Account mapRow(ResultSet rs, int rowNum)
- throws SQLException {
- Account a = new Account();
- a.setId(rs.getInt(1));
- a.setName(rs.getString(2));
- a.setAge(rs.getInt(3));
- a.setSex(rs.getString(4));
- return a;
- }
-
- });
- return matches.size()>0?matches.get(0):null;
- }
-
- }
|
AccountDao 定义了几个账号对象的数据库访问方法:
● saveAccount:负责把传入的账号对象入库
● getAccountById:负责根据 Id 查询账号