spring mybatis 多数据源配置
1、创建好数据库的配置文件
mysql.url=jdbc:mysql://***/***?useUnicode=true&characterEncoding=UTF-8 mysql.username=*** mysql.password=*** mysql.driverClassName=com.mysql.jdbc.Driver dws.url=jdbc:postgresql://***/*** dws.username=*** dws.password=*** dws.driverClassName=org.postgresql.Driver
2、applicationContext.xml文件配置
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启自动扫描--> <context:component-scan base-package="com.huawei.test"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/jdbc.properties</value> </list> </property> </bean> <!--现在可以替换为如下方式--> <!--使用逗号分隔多个值--> <!--<context:property-placeholder location="classpath:config/jdbc.properties"/>--> <!--配置数据源--> <bean id="dataSource_mysql" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> <property name="driverClassName" value="${mysql.driverClassName}"/> </bean> <bean id="sqlSessionFactory_mysql" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_mysql"/> <property name="mapperLocations" value="classpath:mybatis/mysql/*.xml"/> </bean> <bean id="mapperScannerConfigurer_mysql" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.huawei.test.dao.mysql"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_mysql"/> </bean> <!--数据库事务控制--> <bean id="transactionManager_mysql" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_mysql"/> </bean> <bean id="dataSource_dws" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="url" value="${dws.url}"/> <property name="username" value="${dws.username}"/> <property name="password" value="${dws.password}"/> <property name="driverClassName" value="${dws.driverClassName}" /> </bean> <bean id="sqlSessionFactory_dws" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_dws"/> <property name="mapperLocations" value="classpath:mybatis/dws/*.xml"/> </bean> <bean id="mapperScannerConfigurer_dws" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.huawei.test.dao.dws"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_dws"/> </bean> <bean id="transactionManager_dws" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_dws"/> </bean> <!--使用annotation定义事务,比如使用注解--> <tx:annotation-driven/> <mvc:annotation-driven/> </beans>
3、创建好对应的mappers文件