mybatis多数据源配置(依赖引入省略)
配置文件
spring:
datasource:
db1:
url: jdbc:mysql://127.0.0.1:3306/db?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root1
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
db2:
url: jdbc:mysql://127.0.0.2:3306/db?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
username: root2
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置输出到日志文件--生产环境下还是需要谨慎考虑一下是否开启sql日志,因为日志记录是会进行频繁的IO,会有一定的性能损耗。
#log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
mapper-locations: classpath:mapper/*.xml
springboot DB1 config 多文件形式
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "com.zeaho.visualization.mapper.db1",
sqlSessionTemplateRef = "db1SqlSessionTemplate",
annotationClass = Repository.class
)
public class DB1SourceConfig
{
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean
public DataSource db1DataSource(@Qualifier("db1DataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory dataSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception
{
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setPlugins(mybatisPlusInterceptor());
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
return sqlSessionFactory.getObject();
}
@Bean(name = "db1TransactionManager")
@Primary
public DataSourceTransactionManager saasTransactionManager(@Qualifier("db1DataSource") DataSource dataSource)
{
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate saasSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception
{
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionTemplate;
}
/**
* 物理分页配置
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor()
{
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
springboot DB2 config文件
复制上述文件更改bean 名称以及文件名即可
对应创建mapper 下文件 目录 db1 以及 db2