当前位置:Gxlcms > 数据库问题 > springboot整合mybatis+mysql+druid 多个数据源

springboot整合mybatis+mysql+druid 多个数据源

时间:2021-07-01 10:21:17 帮助过:29人阅读

springboot+mybatis+druid 多数据源

1. 创建maven项目,引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.5</version> <!--低版本可以解决mybatis不支持LocalDateTime问题-->
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--druid数据源 start-->
        <!--配置druid多数据源时,要使用druid-spring-boot-starter包,否则会报错
          java: 程序包com.alibaba.druid.spring.boot.autoconfigure不存在
        -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
        <!--druid数据源 end-->

2. 配置多数据源

  1. 配置数据库连接 application.yml
spring:
  datasource:
    # 数据源1
    url: jdbc:mysql://****:3306/dataSourceManagement?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    #数据源2
    icp:
      url: jdbc:mysql://****:3306/icp_rule?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
      username: root
      password: root
      type: com.alibaba.druid.pool.DruidDataSource
  1. 数据源配置类(druid配置,mybatis配置),通过@MapperScan(basePackages = "com.example.datasource.mapper",sqlSessionTemplateRef = "primarySqlSessionTemplate")注解将数据源与mapper文件对应

    1. 数据源1配置类
    @Configuration
    @MapperScan(basePackages = "com.example.datasource.mapper",sqlSessionTemplateRef = "primarySqlSessionTemplate")
    public class PrimaryDataSourceConfig {
    
        /**
        * 数据源配置
        *
        * @return
        */
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        DataSource primaryDs() {
            // 如果使用 druid 作为连接池,则需要使用DruidDataSourceBuilder
            return DruidDataSourceBuilder.create().build();
        }
    
    
        /**
        * mybatis配置SqlSessionFactory和SqlSessionTemplate
        *
        * @return
        */
        @Bean
        SqlSessionFactory primarySqlSessionFactory() {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(primaryDs());
            SqlSessionFactory sqlSessionFactory = null;
            try {
                sqlSessionFactory = sqlSessionFactoryBean.getObject();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sqlSessionFactory;
        }
    
        @Bean
        SqlSessionTemplate primarySqlSessionTemplate() {
            return new SqlSessionTemplate(primarySqlSessionFactory());
        }
    }
    
    1. 数据源2配置类
    @Configuration
    @MapperScan(basePackages = "com.example.datasource.icpMapper", sqlSessionTemplateRef = "icpSqlSessionTemplate")
    public class IcpDataSourceConfig {
    
        /**
        * 数据源配置
        *
        * @return
        */
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource.icp")
        DataSource icpDs() {
            // 如果使用 druid 作为连接池,则需要使用DruidDataSourceBuilder
            return DruidDataSourceBuilder.create().build();
        }
    
    
        /**
        * mybatis配置SqlSessionFactory和SqlSessionTemplate
        *
        * @return
        */
        @Bean
        SqlSessionFactory icpSqlSessionFactory() {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(icpDs());
            SqlSessionFactory sqlSessionFactory = null;
            try {
                sqlSessionFactory = sqlSessionFactoryBean.getObject();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sqlSessionFactory;
        }
    
        @Bean
        SqlSessionTemplate icpSqlSessionTemplate() {
            return new SqlSessionTemplate(icpSqlSessionFactory());
        }
    }
    
  2. 完成,接下来编写mapper,数据源1的mapper写在mapper目录下,数据源2的mapper写在icpMapper目录项,再service层注入即可使用

springboot整合mybatis+mysql+druid 多个数据源

标签:public   prim   pac   conf   cep   stack   ges   password   temp   

人气教程排行