1、在com下面新建一个包ntclportal
2、配置config.properties
a.datasource.type = jdbcDataSource_a
a.datasource.url = jdbc:oracle:thin:@192.168.20.1:1521:test
a.datasource.username = ****
a.datasource.password = ****
a.datasource.jdbc.driver.name = oracle.jdbc.OracleDriver
b.datasource.type = jdbcDataSource_b
b.datasource.url = jdbc:oracle:thin:@192.168.20.2:1521:test2
b.datasource.username = ****
b.datasource.password = ****
b.datasource.jdbc.driver.name = oracle.jdbc.OracleDriver
3、配置applicationContext.xml
<bean id="jdbcDataSource_a" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-
method="close">
<property name="driverClass" value="${a.datasource.jdbc.driver.name}" />
<property name="jdbcUrl" value="${a.datasource.url}" />
<property name="user" value="${a.datasource.username}" />
<property name="password" value="${a.datasource.password}" />
<!-- 最小連接數 -->
<property name="minPoolSize" value="10"/>
<!-- 最大連接數 -->
<property name="maxPoolSize" value="15"/>
<!--最大空閒時間,1800秒內未使用則連結被丟棄。若為0則永不丟棄。Default: 0
-->
<property name="maxIdleTime" value="1800"/>
<!-- 當連接池裡面的連結用完的時候,C3P0一次獲得的新連接數 -->
<property name="acquireIncrement" value="3"/>
<!-- 最大的PreparedStatement數量 -->
<property name="maxStatements" value="1000"/>
<!-- 初始連接數 -->
<property name="initialPoolSize" value="10"/>
<!-- 每隔120秒檢查連接池裡面的空閒連結,單位秒-->
<property name="idleConnectionTestPeriod" value="120"/>
<!-- 從資料庫獲取新連接失敗之後重複嘗試的次數 -->
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="jdbcDataSource_b" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${b.datasource.jdbc.driver.name}" />
<property name="jdbcUrl" value="${b.datasource.url}" />
<property name="user" value="${b.datasource.username}" />
<property name="password" value="${b.datasource.password}" />
<!-- 最小連接數 -->
<property name="minPoolSize" value="10"/>
<!-- 最大連接數 -->
<property name="maxPoolSize" value="15"/>
<!--最大空閒時間,1800秒內未使用則連結被丟棄。若為0則永不丟棄。Default: 0
-->
<property name="maxIdleTime" value="1800"/>
<!-- 當連接池裡面的連結用完的時候,C3P0一次獲得的新連接數 -->
<property name="acquireIncrement" value="3"/>
<!-- 最大的PreparedStatement數量 -->
<property name="maxStatements" value="1000"/>
<!-- 初始連接數 -->
<property name="initialPoolSize" value="10"/>
<!-- 每隔120秒檢查連接池裡面的空閒連結,單位秒-->
<property name="idleConnectionTestPeriod" value="120"/>
<!-- 從資料庫獲取新連接失敗之後重複嘗試的次數 -->
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<!-- ============================================ -->
<!-- Integration Hibernate -->
<!-- ============================================ -->
<!-- org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
<!-- org.springframework.orm.hibernate3.LocalSessionFactoryBean -->
<bean id="sessionFactory_a"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}
</prop>
<prop key="hibernate.max_fetch_depth">5</prop>
<prop key="hibernate.query.substitutions">true 1, false
0</prop>
<!-- connection release mode : JDBC使用after_transaction;
JTA使用after_statement -->
<prop
key="hibernate.connection.release_mode">after_transaction</prop>
<!-- Important : This will drop all tables and create new
tables -->
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
<property name="dataSource" ref="${a.datasource.type}" />
<property name="packagesToScan" value="com.claridy" />
</bean>
<bean id="sessionFactory_b"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}
</prop>
<prop key="hibernate.max_fetch_depth">5</prop>
<prop key="hibernate.query.substitutions">true 1, false
0</prop>
<!-- connection release mode : JDBC使用after_transaction;
JTA使用after_statement -->
<prop
key="hibernate.connection.release_mode">after_transaction</prop>
<!-- Important : This will drop all tables and create new
tables -->
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
<property name="dataSource" ref="${b.datasource.type}" />
<property name="packagesToScan" value="com.claridy" />
</bean>
4、配置SpringDispatcher-servlet.xml
<!-- Scans within the base package of the application for @Components to
configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:annotation-config />
<context:component-scan base-package="com.claridy" />
<context:component-scan base-package="com.ntclportal" />
5、原来的DAO不变,新加的DAO要是使用sessionFactory_b
@Repository
public class PortalHtmlCntDAO extends BaseDAO implements IPortalHtmlCntDAO {
@Autowired
public PortalHtmlCntDAO(SessionFactory sessionFactory_b) {
super.setSessionFactory(sessionFactory_b);
this.createHibernateTemplate(sessionFactory_b);
}
@SuppressWarnings("unchecked")
public List<PortalHtmlCnt> getPortalHtmlCntList(String svcId)
throws DataAccessException {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass
(PortalHtmlCnt.class);
detachedCriteria.add(Restrictions.eq("svcid", svcId));
return (List<PortalHtmlCnt>) this.findByCriteria(detachedCriteria);
}
}
6、记得另一个数据库对应的实体类,service,dao,controller都要放到新建的包下面