为啥要多数据源,因为我现在有个需求,需要将A库的查询结果,写入到B库。
但是原来的springboot里面就只有A库,所以现在要加上B库。
1.修改application.yaml的数据库连接
这个是原来的
现在改成
其实就是将datasource:xxx,改成了datasource:aku:xxx 然后【同级】加了一个bku:xxx。
还有要把url改成jdbc-url 。application的datasource下的url要换成jdbc-url。多数据的话必须换成这个
2.在resource的mapper里创建2个mapper文件夹
将原来默认的mapper下的,内容放到aku下,此时发现变灰,因为mapper路径被改了,下一步会重新配置映射
java代码包mapper的包里,也【创建】2个package
同样,把原来mapper的interface的java文件复制到aku里。
3.修改application.yaml的mybaits配置,重新配置映射
注意红色部分是我新加的,这样就重新映射上了
mybatis: mapper-locations: classpath:/mapper/**/*.xml configuration: map-underscore-to-camel-case: true
4.创建2个源连接,主库和从库的
以前是不需要配置的,因为会走默认配置,现在有2个了,你就得写2个,并且告诉他们对应的mapper是哪个。
随便找个文件夹创建一下,我在我自己的config里面创建下面2个程序
AkuDataSourceConfig
java
package com.example.demo.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.aku", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class AkuDataSourceConfig {
@Primary
@Bean("masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean("masterDataSourceTransactionManager")
public DataSourceTransactionManager masterDataSourceTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/aku/*.xml");
sqlSessionFactory.setMapperLocations(resources);
return sqlSessionFactory.getObject();
}
}
只用注意这3个地方改一下,标红的,分别对应的是interface,mapper.xml还有application.yaml @MapperScan(basePackages = "com.example.demo.mapper.aku", sqlSessionFactoryRef = "masterSqlSessionFactory")
PathMatchingResourcePatternResolver().getResources("classpath:mapper/aku/*.xml");
@ConfigurationProperties(prefix = "spring.datasource.aku")
BkuDataSourceConfig
java
package com.example.demo.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.bku", sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class BkuDataSourceConfig {
@Bean("slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("slaveDataSourceTransactionManager")
public DataSourceTransactionManager slaveDataSourceTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/bku/*.xml");
sqlSessionFactory.setMapperLocations(resources);
return sqlSessionFactory.getObject();
}
}
b库复制这段代码后,需要改的也是类似a库标红的地方。
5.启动测试
成功
用的时候导入不同的mapper,就可以了。