Spring 3 中如果要用DaoSupport的類別, 二種作法如下:
(奇怪的是, Spring Framework Reference Documentation完全沒提DaoSupport)
Annotation的作法如下:
Java Source:
@Repository
public class BaseDao extends SimpleJdbcDaoSupport {
@Autowired
public BaseDao(DataSource dataSource) {
// inject by constructor
setDataSource( dataSource);
}
}
xml:
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.proxool_pool</value>
</property>
</bean>
<!-- not necessary
<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate" id="jdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
--> XML configuration的作法如下:
Java Source:
public class BaseDao extends SimpleJdbcDaoSupport {
// left it empty, inject from xml configuration
}
xml:
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.proxool_pool</value>
</property>
</bean>
<!-- not necessary
<bean class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate" id="jdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
--> <bean class="com.example.BaseDao" id="baseDao">
<property name="dataSource" ref="dataSource" />
</bean>
2011/6/24 更新
Spring 3.1(從3.0M2開始) 把 SimpleJdbcDaoSupport 跟 SimpleJdbcTemplate deprecated 啦~~~
說是整合到NamedParameterJdbcTemplate去了.
參考網址節錄說明: since Spring 3.1 in favor of JdbcTemplate and NamedParameterJdbcTemplate. The JdbcTemplate and NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
Spring in Action 3 的作者哭哭, 書裡都是用SimpleJdbcTemplate來做範例啊.
說NamedParameterJdbcTemplate都整合到SimpleJdbcTemplate 去了.
臉被打得好腫啊~~~ XD
沒有留言:
張貼留言