public class Company { private String name; private Employee managingDirector;
public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Employee getManagingDirector() { return this.managingDirector; } public void setManagingDirector(Employee managingDirector) { this.managingDirector = managingDirector; } }
public class Employee { private float salary;
public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } }
然后我们使用BeanWrapper来调用这两个bean:
Company c = new Company(); BeanWrapper bwComp = BeanWrapperImpl(c); // setting the company name... bwComp.setPropertyValue("name", "Some Company Inc."); // ... can also be done like this: PropertyValue v = new PropertyValue("name", "Some Company Inc."); bwComp.setPropertyValue(v);
// ok, let's create the director and tie it to the company: Employee jim = new Employee(); BeanWrapper bwJim = BeanWrapperImpl(jim); bwJim.setPropertyValue("name", "Jim Stravinsky"); bwComp.setPropertyValue("managingDirector", jim);
// retrieving the salary of the managingDirector through the company Float salary = (Float)bwComp.getPropertyValue("managingDirector.salary");
JDBC再封装JDBC优雅的封装了底层的数据库,但是JDBC仍然存在诸多的不变。你需要编写大量的代码来完成CRUD操作,而且,JDBC无论是遇到什么样的问题,都抛出一个SQLException,这种做法在异常使用上被称为不完备的信息。因为问题可能是很复杂的,也许是数据库连接的问题,也许是并发控制的问题,也许只是SQL语句出错。没有理由用一个简单的SQLException就搞定全部的问题了,这种做法有些不负责任。针对这两个问题,Spring Framework提出了两种解决方法:首先,提供一个框架,把JDBC应用中的获取连接、异常处理、释放等比较通用的操作全部都集中起来,用户只需要提供特定的实现就OK了。实现的具体细节采用的是模板方法。举个例子,在org.springframework.jdbc.object包中,MappingSqlQuery类实现了将SQL查询映射为具体的业务对象。JavaDoc中这样写到:Reusable query in which concrete subclasses must implement the abstract mapRow(ResultSet, int) method to convert each row of the JDBC ResultSet into an object. 用户必须实现mapRow方法,这是典型模板方法的应用。我们拿一个具体的例子来看看:
class UserQuery extends MappingSqlQuery {
public UserQuery(DataSource datasource) { super(datasource, "SELECT * FROM PUB_USER_ADDRESS WHERE USER_ID = ?"); declareParameter(new SqlParameter(Types.NUMERIC)); compile(); }
// Map a result set row to a Java object protected Object mapRow(ResultSet rs, int rownum) throws SQLException { User user = new User(); user.setId(rs.getLong("USER_ID")); user.setForename(rs.getString("FORENAME")); return user; }
public User findUser(long id) { // Use superclass convenience method to provide strong typing return (User) findObject(id); } }
JdbcTemplate template = new JdbcTemplate(dataSource); final List names = new LinkedList(); template.query("SELECT USER.NAME FROM USER", new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { names.add(rs.getString(1)); } });