spring mvc + mybatis 连接同一地址的多个数据库
2017-11-06 17:02
288 查看
1. 修改数据库配置mysql.properties文件
2. 修改mybatis配置 spring-mybatis.xml文件(dataSourceOld、dataSourceNew、dataSource)
3. 创建类DbcontextHolder
4. 在方法中连接不同数据库
driver=com.mysql.jdbc.Driver oldmysql.url=jdbc:mysql://localhost:3306/spider_changed?tinyInt1isBit=false newmysql.url=jdbc:mysql://localhost:3306/db_hbrb_spider_async?tinyInt1isBit=false username=root password=root #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
2. 修改mybatis配置 spring-mybatis.xml文件(dataSourceOld、dataSourceNew、dataSource)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.spiderweb"> </context:component-scan> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:mysql.properties" /> </bean> <bean id="dataSourceOld" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${oldmysql.url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <bean id="dataSourceNew" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${newmysql.url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <bean id="dataSource" class="com.spiderweb.common.DynamicDataSource"><!-- 这里写选择数据源的类地址 下面跟着给出--> <property name="defaultTargetDataSource" ref="dataSourceOld"/><!-- 设置默认为此dataSourceOld数据源--> <property name="targetDataSources"> <map> <entry key="dataSourceOld" value-ref="dataSourceOld"/> <entry key="dataSourceNew" value-ref="dataSourceNew"/> </map> </property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/spiderweb/mapping/*/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.spiderweb.dao.mapper.*" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
3. 创建类DbcontextHolder
package com.spide a3e1 rweb.common; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DbcontextHolder extends AbstractRoutingDataSource { public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); /** * 设置当前数据源 * @param dbType */ public static void setDbType(String dbType){ contextHolder.set(dbType); } /** * 获得当前数据源 * @return */ public static String getDbType(){ String dbType = (String)contextHolder.get(); return dbType; } /** *清除上下文 * */ public void clearContext(){ contextHolder.remove(); } @Override protected Object determineCurrentLookupKey() { return DbcontextHolder.getDbType(); } }
4. 在方法中连接不同数据库
//连接dataSourceOld进行查询 DbcontextHolder.setDbType("dataSourceOld"); Template template = templateMapperExtend.selectByPrimaryKey(templateId); //连接dataSourceNew进行插入 DbcontextHolder.setDbType("dataSourceNew"); siteAsyncMapper.insertSelective(siteAsync);
相关文章推荐
- spring+springmvc+mybatis整合连接不上数据库,求大神帮忙
- Spring+SpringMVC+MyBatis实现数据库连接的登录功能
- echart+ajax动态读取数据库常用的数据化图表(spring+springMVC+Mybatis)
- Spring Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
- spring+spring mvc +mybatis+druid 实现数据库主从分离
- 利用POI导入Excel数据到数据库,基于Spring+SpringMVC+Mybatis
- SpringBootl连接数据库MyBatis方式实现
- SpringMVC+ Mybatis 配置多数据源 + 自动数据源切换 + 实现数据库读写分离
- Spring+Mybatis+SpringMVC+Atomikos多数据源共存+不同数据库事物一致性处理
- Spring-Mybatis整合时,无法连接数据库,Cannot create PoolableConnectionFactory
- springboot+springmvc+mybatis 使用注解对数据库的增、改和查操作
- spring整合mybatis 报错无法连接数据库
- spring mvc + spring + mybatis 查询数据库后取值为null
- 【系列】使用springmvc+mybatis创建Web应用(二)—— 数据库、配置和测试
- 搭建SpringMVC+Mybatis框架并实现数据库的操作
- SpringBoot - MyBatis 连接数据库
- Spring+SpringMVc+Mybatis实现数据库查询
- springmvc+mybatis 数据库配置
- Maven+SpringMVC+Spring+MyBatis连接MySQL数据库详细搭建整合过程
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(七)JDBC url的连接参数