时间:2021-07-01 10:21:17 帮助过:140人阅读
resin文件配置(我用的resin,其他的可自行配置)
<database>
<jndi-name>jdbc/OracleDB</jndi-name>
<driver type="oracle.jdbc.driver.OracleDriver">
<!--<url>jdbc:oracle:thin:@oa.paipai580.com:1521:orcl</url>
<user>cggl</user>
<password>cggl345#$%</password>-->
<url>jdbc:oracle:thin:@192.168.1.9:1521:orcl</url>
<user>ycg</user>
<password>ycg</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
<database>
<jndi-name>jdbc/mysqlDB</jndi-name>
<driver type="com.mysql.jdbc.Driver">
<url>jdbc:mysql://39.98.47.52:3306/dev-idr</url>
<user>admin</user>
<password>mySteel2019,!@#</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
1. 然后写一个数据源持有类:
package org.thcic.ejw.components.datatable;
public class DataSourceHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
/**
* @Description: 设置数据源类型
* @param dataSourceType 数据库类型
* @return void
* @throws
*/
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
/**
* @Description: 获取数据源类型
* @return String
* @throws
*/
public static String getDataSourceType() {
return contextHolder.get();
}
/**
* @Description: 清除数据源类型
* @return void
* @throws
*/
public static void clearDataSourceType() {
contextHolder.remove();
}
}
2. 写一个数据源路由类
package org.thcic.ejw.components.datatable;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSourceType();
}
}
3. 配置数据源路由
<!--配置数据源路由-->
<bean id="dataSource" class="org.thcic.ejw.components.datatable.RoutingDataSource">
<!-- 为targetDataSources注入两个数据源 -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="orcleDb" value-ref="orclDataSource"/>
<entry key="mysqlDb" value-ref="mysqlDataSource"/>
</map>
</property>
<!-- 为指定数据源RoutingDataSource注入默认的数据源-->
<property name="defaultTargetDataSource" ref="orclDataSource"/>
</bean>
4. spring整合
<!--hibernate start-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dataSource.hibernate.dialect}</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">utf-8</prop>
<prop key="hibernate.show_sql">${dataSource.hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>org.thcic.ejw.sys.vo</value>
<value>org.thcic.ejw.purchase.setting.vo</value>
<value>org.thcic.ejw.purchase.inquiry.vo</value>
<value>org.thcic.ejw.purchase.notice.vo</value>
<value>org.thcic.ejw.purchase.bid.vo</value>
<value>org.thcic.ejw.purchase.report.vo</value>
</list>
</property>
</bean>
5.双jdbcTemplate配置
<!--jdbcTemplate start-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="mysqlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource" ref="mysqlDataSource"></property>
</bean>
6.使用数据源
/*jdbcTemplate的使用*/
@Autowired
@Qualifier("oracleJdbcTemplate")
private JdbcTemplate dbTwojdbcTemplate;
@Autowired
@Qualifier("mysqlJdbcTemplate")
private JdbcTemplate dbOneJdbcTemplate;
/*数据源切换*/
//获取数据源类型
DataSourceHolder.getDataSourceType();
//设置为 db1 的数据源
DataSourceHolder.setDataSourceType("db1");
//设置为 db2 的数据源
DataSourceHolder.setDataSourceType("db2");
双数据源配置(一个项目中链接两个数据库)
标签:new routing 文件 ice scan osc red lookup void