SSH配置多数据源(JDBC连接)
0.把用到的 JDBC 包导进来
1.创建类DataSourceMap
public class DataSourceMap { public static final String ds1= "ds1"; public static final String ds2= "ds2"; }
2.创建类CustomerContextHolder
import java.util.Map; import com.FrameWork.db.interf.IDAO; public class CustomerContextHolder { private Map<String,IDAO> map =null; public Map<String, IDAO> getMap() { return map; } public void setMap(Map<String, IDAO> map) { this.map = map; } }
3.配置连接池
<!-- 程序自身连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.1.188:1521:orcl" /> <property name="username" value="ddd" /> <property name="password" value="xxxx" /> <property name="maxActive" value="30" /> <property name="maxIdle" value="30" /> <property name="maxWait" value="1000" /> <property name="defaultAutoCommit" value="true" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="60" /> </bean> <!-- sql server 2000 car数据库 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=car"/> <property name="username" value="sa"/> <property name="password" value="sa"/> <property name="maxActive" value="30"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="60"/> </bean> <!-- sql server 2000 gps数据库 --> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=gps"/> <property name="username" value="sa"/> <property name="password" value="sa"/> <property name="maxActive" value="30"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="60"/> </bean>
4.设置数据库连接池
<!-- Generic IDao - can be used when doing standard CRUD--> <bean id="idao" class="com.FrameWork.db.impl.DAOImpl"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="idao1" class="com.FrameWork.db.impl.DAOImpl"> <property name="dataSource" ref="dataSource1" /> </bean> <bean id="idao2" class="com.FrameWork.db.impl.DAOImpl"> <property name="dataSource" ref="dataSource2" /> </bean>
5.分配连接池
<!-- Construct Map --> <bean id="nameMap" class="java.util.HashMap"> <constructor-arg> <map> <entry key="ds1" value-ref="idao1"/> <entry key="ds2" value-ref="idao2"/> </map> </constructor-arg> </bean> <bean id="customerContextHolder" class="com.FrameWork.dynamicDataSource.CustomerContextHolder"> <property name="map"> <ref bean="nameMap"></ref> </property> </bean>
6.配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <!-- 这里要注意--> <ref bean="dataSource" /> </property> <property name="lobHandler" ref="lobHandler" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <!-- <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> --> </props> </property> <!--Spring Blob数据类型支持--><!-- <property name="lobHandler" ref="oracleLobHandler" /> --><!--hbm.xml映射文件在此处注册--> <property name="mappingResources"> <list> <!-- 库房管理 --> <value> com/czkj/event/wzgl/mapping/TWzDepot.hbm.xml </value> <value> com/czkj/event/wzgl/mapping/TWzCkgly.hbm.xml </value> .... </list> </property> </bean>
7.spring 注入
<!-- 数据同步 --> <bean id="synCarImp" class="com.czkj.service.syn.SynCarImp"> <property name="dynDataSource" ref="customerContextHolder"></property> <property name="idao" ref="idao"></property> <property name="ihibernatedao" ref="ihibernatedao"></property> <property name="sctx" ref="SystemContext"></property> </bean>
8.java代码调用 类中声明
// 配置多数据源操作 声明 private CustomerContextHolder dynDataSource = null; public void setDynDataSource(CustomerContextHolder dynDataSource) { this.dynDataSource = dynDataSource; }
9.调用
public void test(){ List list = dynDataSource.getMap().get(DataSourceMap.ds2).queryforlist("select * from FuncMain "); System.out.println(list); }
10.完成
相关推荐
ASoc 2020-11-14
Andrea0 2020-09-18
Cherishyuu 2020-08-19
dongtiandeyu 2020-08-18
CoderYYN 2020-08-16
大黑牛 2020-08-15
Dullonjiang 2020-08-11
gaozhennan 2020-08-03
mcvsyy 2020-08-02
zbcaicai 2020-07-29
AscaryBird 2020-07-27
liulin0 2020-07-26
ldcwang 2020-07-26
helloxusir 2020-07-25
娜娜 2020-07-20
pengpengflyjhp 2020-07-19
点滴技术生活 2020-07-19
人可 2020-07-18
chenjiazhu 2020-07-08