zoukankan      html  css  js  c++  java
  • SpringBoot+mybatis多数据源配置

     1.需要alibaba 的数据源的maven

     <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>

    2.在yml或properties配置数据源(我的是yml)

    server:
      port: 8085
    
    
    spring:
      datasource:
        files:
          driverClassName: com.mysql.jdbc.Driver
          username: root
          password: root
          # spring2.0此处为jdbc-url
          jdbc-url: jdbc:mysql:///hpt_files?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
          type: com.alibaba.druid.pool.DruidDataSource
        center:
          driverClassName: com.mysql.jdbc.Driver
          username: root
          password: root
          # spring2.0此处为jdbc-url
          jdbc-url: jdbc:mysql:///hpt_controlcenter?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
          type: com.alibaba.druid.pool.DruidDataSource
      mvc:

    3.因为有两个数据源,所以我们需要把:mapper.xml,mapper(也是以前的dao接口),放入到不同的目录下,创建两个数据源的datasource

    4.在datasource中,第一个数据源配置(必须指定主数据源!)

    package com.wom.file.datasource;
    
    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.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 javax.sql.DataSource;
    @Configuration//注解到spring容器中
    @MapperScan(basePackages ="com.wom.file.mapper.files", sqlSessionTemplateRef  = "hptFilesSqlSessionTemplate")
    public class FilesDataSource {
    
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource.files")
        @Primary
        public DataSource hptFilesDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        @Primary
        public SqlSessionFactory hptFilesSqlSessionFactory(@Qualifier("hptFilesDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            // 开启驼峰命名
            //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/files/*.xml"));
            return bean.getObject();
        }
    
        @Bean
        @Primary
        public DataSourceTransactionManager db1TransactionManager(@Qualifier("hptFilesDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean
        @Primary
        public SqlSessionTemplate hptFilesSqlSessionTemplate(@Qualifier("hptFilesSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    5.配置第二个数据源:

    package com.wom.file.datasource;
    
    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.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration//注解到spring容器中
    @MapperScan(basePackages ="com.wom.file.mapper.controlcenter", sqlSessionTemplateRef  = "centerSqlSessionTemplate")
    public class ControlcenterDataSource {
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource.center")
        public DataSource centerDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        public SqlSessionFactory centerSqlSessionFactory(@Qualifier("centerDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            // 开启驼峰命名
           // bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/controlcenter/*.xml"));
            return bean.getObject();
        }
    
        @Bean
        public DataSourceTransactionManager db2TransactionManager(@Qualifier("centerDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean
        public SqlSessionTemplate centerSqlSessionTemplate(@Qualifier("centerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    6.到此就结束。佛祖保佑,永不BUG!

  • 相关阅读:
    流程数据库的归档
    [转载]利用老毛桃WinPE制作启动U盘安装系统
    [转载]分享日志 Word,PDF,PPT,TXT之间的转换方法
    编程书籍推荐(转)
    ArcGIS教程下载 系列 ArcMap教程下载 ArcCatlog 教程下载 等的学习资料下载 (google文档 可以直接查看 也可以下载)
    JDK 1.6 下载 地址
    (转)MapXtreme for Java 精华文章
    Java2D Tutorial(方便自己找)
    JFC:Java
    转自百度百科《OpenGL》
  • 原文地址:https://www.cnblogs.com/www-yang-com/p/15252143.html
Copyright © 2011-2022 走看看