領先一步
VMware 提供培訓和認證,以加速您的進度。
了解更多在The Spring Experience的籌備期間,我一直很忙,但我注意到 Rod 在部落格方面非常活躍。所以在機場和飛機上的一些空閒時間,我決定寫一篇部落格。
我們 Spring 社群中最大的平衡工作之一,就是確保我們在創新的同時,保持向後相容。 這種創新的一部分是利用 Java 後續版本(例如 Java 5)中的新功能和結構。 自 1.2.x 分支以來,我們已經看到一些這樣的例子,像是 @Transactional 註解和基於 @ManagedResource 註解的 JMX 自動偵測。 最後,這些都是很棒的功能,並且大大簡化了開發(至少對我來說是這樣),但它們實際上相當於將元數據移到程式碼中。 我們沒有看到的是 API 的實際簡化。
在 Spring 2.0 中,情況發生了變化。 我們看到某些 API 利用了除註解之外的功能。 實際上,有一個很好的例子,我們看到了 Java 5 的幾乎每個新語言功能(自動裝箱、可變參數、泛型),也就是 SimpleJdbcTemplate。SimpleJdbcTemplate 並不是標準 JdbcTemplate 的替代品,而是使用 Java 5 來簡化某些常見任務。
public int getLargeAccountCount(double value, int type) {
return getSimpleJdbcTemplate().queryForInt(
"select count(*) from accounts where balance > ? and type = ?",
new Object[] { new Double(value), new Integer(type) });
}
… 現在您不必這樣做了。
public int getLargeAccountCount(double value, int type) {
return getSimpleJdbcTemplate().queryForInt(
"select count(*) from accounts where balance > ? and type = ?",
new Object[] { value, type });
}
在這個簡單的範例中,它沒有太大的區別,但您可以想像在一個具有多個繫結變數的複雜 SQL 查詢中,裝箱可能會佔用相當多的空間。
因此,如果我們從原始範例開始…
public int getLargeAccountCount(double value, int type) {
return getSimpleJdbcTemplate().queryForInt(
"select count(*) from accounts where balance > ? and type = ?",
new Object[] { new Double(value), new Integer(type) });
}
… 並結合自動裝箱和可變參數,事情真的開始變得更簡短。
public int getLargeAccountCount(double value, int type) {
return getSimpleJdbcTemplate().queryForInt(
"select count(*) from accounts where balance > ? and type = ?",
value, type);
}
public Account getAccount(long id) {
return (Account) getJdbcTemplate().queryForObject(
"select * from accounts where id = ?",
new Object[] { new Long(id) }, new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum)
throws SQLException {
String accountNumber = rs.getString("account_number");
int balance = rs.getInt("balance");
return new Account(accountNumber, balance);
}
});
}
在 Spring 2.0 中,我們發布了 SimpleJdbcTemplate 的配套元件,ParameterizedRowMapper。 當兩者一起使用時,它實際上會創建一個非常好的小系統,根本不需要轉換,並且讓您的 IDE 和編譯器執行強型別檢查。
public Account getAccount(long id) {
return getSimpleJdbcTemplate().queryForObject(
"select * from accounts where id = ?",
new ParameterizedRowMapper<Account>() {
public Account mapRow(ResultSet rs, int rowNum)
throws SQLException {
String accountNumber = rs.getString("account_number");
int balance = rs.getInt("balance");
return new Account(accountNumber, balance);
}
}, id);
}
要記住的一件事是,SimpleJdbcTemplate 並不具備 JdbcTemplate 的所有方法。 它甚至沒有擴展 JdbcTemplate,而是可以提供對 JdbcTemplate 的引用。SimpleJdbcTemplate 的目標是簡化某些常見行為的使用,同時利用 Java 5。
最後,這沒有什麼革命性的。 就像 Rod 的 先前文章一樣,這只是語法糖。 但這是一個 Spring 正在擁抱 Java 5 及更高版本中新功能的例子。