spring,mybatis配多个数据库
- 2019 年 10 月 30 日
- 筆記
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/luo4105/article/details/51742654
1.在jdbc配置文件jdbc.properties中写好两个数据库地址
#jdbc settings jdbc.driver=com.mysql.jdbc.Driver ########################################################## jdbc.url=jdbc:mysql://ip地址:3306/数据库名?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=123456 #dbcp settings dbcp.initialSize=5 dbcp.maxIdle=30 dbcp.maxActive=100 dbcp.maxWait=1000 ho.jdbc.driver=com.mysql.jdbc.Driver #ho.jdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull ho.jdbc.url=jdbc:mysql://ip地址:3306/energy_monit?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull ho.jdbc.username=root #ho.jdbc.password=123456 ho.jdbc.password=123456 ho.dbcp.initialSize=5 ho.dbcp.maxIdle=30 ho.dbcp.maxActive=100 ho.dbcp.maxWait=1000
2.在spring文件中配置两个数据源
<!-- 读取文件 --> <context:property-placeholder location="classpath:com/erpto/cle/config/jdbc.properties" /> <!-- 数据源配置1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <!-- mapper扫描1 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:/com/erpto/cle/config/mybatis/**/*.xml" /> </bean> <!-- 数据源配置2 --> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${ho.jdbc.driver}" /> <property name="url" value="${ho.jdbc.url}" /> <property name="username" value="${ho.jdbc.username}" /> <property name="password" value="${ho.jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${ho.dbcp.initialSize}" /> <property name="maxIdle" value="${ho.dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <!-- mapper扫描2 --> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource1" /> <property name="mapperLocations" value="classpath*:/com/erpto/cle/config/mybatis2/**/*.xml" /> </bean> <!-- dao包配置1 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.erpto.cle.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- dao包配置2 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.erpto.cle.dao2" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"></property> </bean> <!-- 事务配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务配置 --> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource1"></property> </bean> <!-- 没有配置事务管理器,以后加上 lc 2016-6-14 -->