<!-- lang: xml --> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">mysql
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/new"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="com/mydomain/data/SqlMapConfig.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- 第一種將sqlMapClient直接注入到dao實現類中,並繼承SqlMapClientDaoSupport dao實現類繼承自SqlMapClientDaoSupport--> <!-- <bean id="accountDao" class="example.SqlMapAccountDao"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> --> <!-- 第二種將sqlMapClient直接注入到sqlMapClientDaoSupport實現類中將sqlMapClientDaoSupport的bean定義爲抽象,dao的bean利用parent繼承抽象bean sqlMapClientDaoSupport,dao類繼承SqlMapClientDaoSupport dao實現類繼承自SqlMapClientDaoSupport--> <!-- <bean id="sqlMapClientDaoSupport" class="org.springframework.orm.ibatis.support.SqlMapClientDaoSupport" abstract="true"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="accountDao" class="example.SqlMapAccountDao" parent="sqlMapClientDaoSupport"> </bean> --> <!-- 第三種定義一個抽象bean,有一個屬性爲工廠類,即sqlMapClient,在使dao bean繼承abstractDao bean,dao實現類繼承自SqlMapClientDaoSupport--> <!-- <bean id="abstractDao" abstract="true" > <property name="sqlMapClient" ref="sqlMapClient"/>
</bean> <bean id="accountDao" class="example.SqlMapAccountDao" parent="abstractDao"> </bean> --> <!-- 第四種將sqlMapClient直接注入到dao實現類中,並繼承SqlMapClientTemplate bean繼承自SqlMapClientTemplate--> <!-- <bean id="accountDao" class="example.SqlMapAccountTemplateDao"> <property name="sqlMapClient" ref="sqlMapClient"/> </bean> --> <!-- 第五種將sqlMapClient直接注入到sqlMapClientTemplate,並繼承SqlMapClientTemplate bean繼承自SqlMapClientTemplate,同第二種--> <!-- <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" abstract="true"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="accountDao" class="example.SqlMapAccountTemplateDao" parent="sqlMapClientTemplate"> </bean> --> <!-- 第六種定義一個抽象bean,有一個屬性爲工廠類,即sqlMapClient,在使dao bean繼承abstractDao bean,dao實現類繼承自SqlMapClientTemplate--> <!-- <bean id="abstractDao" abstract="true" > <property name="sqlMapClient" ref="sqlMapClient"/> </bean> <bean id="accountDao" class="example.SqlMapAccountTemplateDao" parent="abstractDao"> </bean> --> <!-- 第七種手動在dao實現類中定義一個SqlMapClientTemplate的屬性,提供set方法,並對其進行注入sqlMapClientTemplate,並將sqlMapClient注入進SqlMapClientTemplate類中 --> <!-- <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" > <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="accountDao" class="example.SqlMapAccountBaseDao" > <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/> </bean> --> <!-- 它沒有相似於第二種的方式,由於它沒有繼承任何類 --> <!-- 第八種相似於第七種加第三種的的方式 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate" > <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="abstractDao" abstract="true" > <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/> </bean> <bean id="accountDao" class="example.SqlMapAccountBaseDao" parent="abstractDao"> </bean> <!-- 第九種是手動定義SqlMapClientDaoSupport爲dao實現類的屬性,並將sqlMapClient注入SqlMapClientDaoSupport中,配置和代碼省略 --> <!-- 第十種是第九種變成抽象的,配置和代碼省略 -->spring
</beans>