在springboot项目中,使用单一数据源时,一个典型的配置如下:
spring.datasource.url=jdbc:mysql://ipaddress:port/dbname?characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true
spring.datasource.username=usernane
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapperxml文件路径/*.xml
mybatis.config-location=classpath:mybatis-config.xml
在改为使用多数据源时,需要做的几个工作如下:
1:修改application.properties文件:
1.1 由于不同的数据源对应的mapper接口和xml文件需要放到不同的路径下,因此上面配置中的mybatis.mapper-locations需要去掉;
1.2 配置不同数据源的数据库连接信息:
spring.datasource.ds1.url=jdbc:mysql://ip1address:port/dbname?characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true
spring.datasource.ds1.username=usernane1
spring.datasource.ds1.password=password1
spring.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.ds2.url=jdbc:mysql://ip2address:port/dbname?characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&useOldAliasMetadataBehavior=true
spring.datasource.ds2.username=usernane2
spring.datasource.ds2.password=password2
spring.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
2:定义每个数据源的DataSource bean、sqlsession factory bean、sql session templatebean和data transaction bean
建立第一个数据源的JAVA配置类如下:
1 package com.demo.datasources; 2 3 import org.apache.ibatis.session.SqlSessionFactory; 4 import org.mybatis.spring.SqlSessionFactoryBean; 5 import org.mybatis.spring.SqlSessionTemplate; 6 import org.mybatis.spring.annotation.MapperScan; 7 import org.springframework.beans.factory.annotation.Qualifier; 8 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 9 import org.springframework.boot.context.properties.ConfigurationProperties; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.context.annotation.Primary; 13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 16 import javax.sql.DataSource; 17 18 @Configuration 19 @MapperScan(basePackages = "com.demo.dao1", sqlSessionFactoryRef = "sqlSessionFactory1") 20 public class DataSourceConfigOne { 21 /** 22 * 数据源1 23 * @return 24 */ 25 @Bean(name = "datasource1") 26 @Primary 27 @ConfigurationProperties(prefix = "spring.datasource.ds1") 28 public DataSource getDataSourceOne(){ 29 return DataSourceBuilder.create().build(); 30 } 31 32 /** 33 * 会话工厂1 34 */ 35 @Bean(name = "sqlSessionFactory1") 36 @Primary 37 public SqlSessionFactory sqlSessionFactory1(@Qualifier("datasource1") DataSource dataSource) throws Exception{ 38 SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 39 sqlSessionFactoryBean.setDataSource(dataSource); 40 sqlSessionFactoryBean.setMapperLocations( 41 new PathMatchingResourcePatternResolver().getResources("classpath*:com/demo/dao1/*.xml")); 42 return sqlSessionFactoryBean.getObject(); 43 } 44 45 /** 46 * 会话Template1 47 */ 48 @Bean(name = "sqlSessionTemplate1") 49 @Primary 50 public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception{ 51 return new SqlSessionTemplate(sqlSessionFactory); 52 } 53 54 /** 55 * 返回数据库1事务 56 */ 57 @Bean(name = "databaseSessionTransaction1") 58 @Primary 59 public DataSourceTransactionManager transactionManager(@Qualifier("datasource1") DataSource dataSource){ 60 return new DataSourceTransactionManager(dataSource); 61 } 62 }
上面代码中可以看出,application.properties中配置的不同数据源的数据库连接信息,是通过@ConfigurationProperties(prefix = "spring.datasource.ds1")注解来为数据源提供连接信息。
@MapperScan(basePackages = "com.demo.dao1", sqlSessionFactoryRef = "sqlSessionFactory1")注解则指定了mapper扫描路径。
会话工厂bean中的:
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/demo/dao1/*.xml"));指定了xml的读取路径。
最后确保mapper.xml文件中namespace指定对应的接口类名称即可。
测试无问题后,以同样的方式建立第二个数据源即可。
最后补充一点:如果项目运行时提示 Invalid bound statement (not found) 错误,请检查生成的jar包里面是否正确的打包了mapper.xml文件,特别是你的mapper.xml文件不是位于resources目录下,而是位于java包中时,如果没有在maven的pom文件中特别指定资源目录,很容易导致mapper.xml文件未打包到jar中,从而引发 Invalid bound statement (not found)错误。解决办法如下:
在pom文件的build节点下,增加配置(黄色高亮部分):
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,devtools 不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>