bean定義:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中裝載sqlMapClient並建立sqlMapClientfactory,每次執行sql操作都從factory中獲取sqlMapClient.factory獲取bean方法:
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每個DAO中使用spring註解註入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.創建BaseDao繼承SqlMapClientDaoSupport,所有DAO都繼承BaseDao.BaseDao中註入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
該方法實際上是使用了sqlMapclientTemplate並向template註入sqlMapClient。