springboot(2.0) mybatis 多数据源配置

mybatis 多数据源配置

github:mybatis多数据源配置
项目名:springboot-mybaits-mulidata

注意事项:

  1. 重点:多数据源配置时,要指定一个数据源为主数据源并使用@Primary注解
  2. @MapperScan(basePackages)指定mapper文件的位置和SqlSessionFactory中指定xml文件的位置
  3. dataSource配置顺序:DataSource(数据源),SqlSessionFactory(session工厂),DataSourceTransactionManager(数据库事务),SqlSessionTemplate(操作数据库方法的封装类)

1.项目结构和配置流程

springboot(2.0) mybatis 多数据源配置_第1张图片

1.1配置流程

  1. 生成实体类
  2. 创建mapper接口和xml文件
  3. 配置application.properties
  4. 配置dataSource

2.配置说明

application.properties配置

server.port=8080

mybatis.type-aliases-package=com.plumblum.entity

logging.level.com.plumblum.mapper=debug

spring.datasource.test1.driver-Class-Name = com.mysql.jdbc.Driver
#多数据源时,为jdbc-url
spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = 0806

.spring.datasource.test2.driver-Class-Name = com.mysql.jdbc.Driver
#多数据源时,为jdbc-url
spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = 0806

多数据源1配置

@Configuration
@MapperScan(basePackages = "com.plumblum.mapper.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config {

    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath*:mappers/test1/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

多数据源2配置

@Configuration
@MapperScan(basePackages = "com.plumblum.mapper.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource2Config {

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath*:mappers/test2/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

3测试和结果展示

测试方法
springboot(2.0) mybatis 多数据源配置_第2张图片
springboot(2.0) mybatis 多数据源配置_第3张图片
测试结果

这里写图片描述

这里写图片描述

你可能感兴趣的:(SpringBoot,springboot,学习)