美文网首页
SpringBoot使用mybatis-plus配置多个数据源,

SpringBoot使用mybatis-plus配置多个数据源,

作者: chenzan | 来源:发表于2021-07-14 15:13 被阅读0次

    SpringBoot使用mybatis-plus配置多个数据源,实现项目中从两个不同的数据库读取数据

    1、添加依赖包

    <properties>
        <java.version>1.8</java.version>
        <mybatis.version>2.1.2</mybatis.version>
        <mybatis.plugin.version>3.4.3</mybatis.plugin.version>
        <pagehelper.version>1.2.13</pagehelper.version>
    </properties>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plugin.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>${pagehelper.version}</version>
    </dependency>
    

    2、禁止SpringBoot自动配置数据源

    在启动类注解上添加exclude参数

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

    3、配置文件application.yml添加数据库信息

    spring:
      datasource:
        db1:
          type: com.zaxxer.hikari.HikariDataSource
          driverClassName: com.mysql.cj.jdbc.Driver
          jdbcUrl: jdbc:mysql://172.16.253.52:3306/aaa?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: root
        db2:
          type: com.zaxxer.hikari.HikariDataSource
          driverClassName: com.mysql.cj.jdbc.Driver
          jdbcUrl: jdbc:mysql://172.16.253.55:3306/bbb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: root
    
    

    4、代码中配置db1和db2的数据源

    新建类DataSourceConfig1

    配置MapperLocation路径和MapperScan扫描路径

    @Slf4j
    @Configuration
    @MapperScan(basePackages = "com.demo.mapper.db1.**", sqlSessionFactoryRef = "sqlSessionFactoryDB1")
    public class DataSourceConfig1 {
        
        @Primary
        @Bean(name = "dataSourceDB1")
        @ConfigurationProperties(prefix = "spring.datasource.db1")
        public DataSource dataSource() {
            return DataSourceBuilder.create().type(HikariDataSource.class)
                    .build();
        }
    
        @Bean(name = "sqlSessionFactoryDB1")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourceDB1") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);
            sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mapper/db1/*.xml"));
            log.info("------dataSource db12 配置成功");
            return sessionFactoryBean.getObject();
        }
    
        @Bean(name = "sqlSessionTemplateDB1")
        public SqlSessionTemplate sqlSessionFactoryTemplate(@Qualifier("sqlSessionFactoryDB1") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    
        @Bean(name = "txManagerDB1")
        public PlatformTransactionManager txManager(@Qualifier("dataSourceDB1") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }
    
    

    新建类DataSourceConfig2

    @Slf4j
    @Configuration
    @MapperScan(basePackages = "com.demo.mapper.db2.**", sqlSessionFactoryRef = "sqlSessionFactoryDB2")
    public class DataSourceConfig2 {
        
        @Primary
        @Bean(name = "dataSourceDB2")
        @ConfigurationProperties(prefix = "spring.datasource.db2")
        public DataSource dataSource() {
            return DataSourceBuilder.create().type(HikariDataSource.class)
                    .build();
        }
    
        @Bean(name = "sqlSessionFactoryDB2")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourceDB2") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(dataSource);
            sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mapper/db2/*.xml"));
            log.info("------dataSource db2 配置成功");
            return sessionFactoryBean.getObject();
        }
    
        @Bean(name = "sqlSessionTemplateDB2")
        public SqlSessionTemplate sqlSessionFactoryTemplate(@Qualifier("sqlSessionFactoryDB2") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    
        @Bean(name = "txManagerDB2")
        public PlatformTransactionManager txManager(@Qualifier("dataSourceDB2") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }
    

    至此配置完成

    相关文章

      网友评论

          本文标题:SpringBoot使用mybatis-plus配置多个数据源,

          本文链接:https://www.haomeiwen.com/subject/mtpypltx.html