Spring+Ibatis 与 Spring+Hibernate的区别在于Ibatis和Hibernate的配置不同,下面来看下Ibatis的配置
<jee:jndi-lookup id="ds0" jndi-name="jdbc/ds0"/> <jee:jndi-lookup id="ds1" jndi-name="jdbc/ds1"/> <jee:jndi-lookup id="ds2" jndi-name="jdbc/ds2"/> <bean id="dataSource" class="com.xxx.xxx.util.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry key="1" value-ref="ds1"/> <entry key="2" value-ref="ds2"/> </map> </property> <property name="defaultTargetDataSource" ref="ds0"/> </bean> <!-- SqlMap setup for iBATIS Database Layer --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:com/xxx/xxx/dao/sqlmap/sql-map-config.xml"/> </bean>
sql-map-config.xml 如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="com/xxx/xxx/dao/sqlmap/Object.xml"/> </sqlMapConfig>
核心是DynamicDataSource 与 上下文DynamicDataSourceHolder为一线程安全的ThreadLocal,都和上一篇文章的中对应的类相同。