• springboot druid 多数据源配置


    由于工作当中,需要配置双数据源进行数据同步,以目录进行区别主副库的编写。这里记录一下,方便大家。

    1、pom配置

       

    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>${druid.version}</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>${druid-spring-boot-starter.version}</version>
    </dependency>

     

    2、yml配置

    spring:
    datasource:
    druid:
    primary:
    driver-class-name: oracle.jdbc.driver.OracleDriver
           url: jdbc:oracle:thin:@192.168.1.1:1521:orcl # 数据库名称
           username: test
    password: 123456
    min-idle: 1 # 数据库连接池的最小维持连接数
           initial-size: 5 # 初始化连接数
           max-active: 8
    max-wait: 60000
    secondary:
    driver-class-name: oracle.jdbc.driver.OracleDriver # 驱动包 todo 光谷库
           url: jdbc:oracle:thin:@192.168.1.2:1521:orcl # 数据库名称
           username: test
    password: inner
    min-idle: 1 # 数据库连接池的最小维持连接数
           initial-size: 5 # 初始化连接数
           max-active: 8
    max-wait: 60000

    3、配置类编写 

       

    @Configuration
    public class DataSourceConfig {
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.primary")
    @Primary
    public DruidDataSource primaryDataSource() {
    return DruidDataSourceBuilder.create().build();
    }
    @Bean
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    bean.setMapperLocations(resolver.getResources(
    "classpath*:mybatis/mapping/primary/*.xml"));
    bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis.cfg.xml"));
    return bean.getObject();
    }
    @Bean
    @Primary
    public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
    }






    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.secondary")
    public DruidDataSource secondaryDataSource() {
    return DruidDataSourceBuilder.create().build();
    }
    @Bean
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    bean.setMapperLocations(resolver.getResources(
    "classpath*:mybatis/mapping/secondary/*.xml"));
    bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis.cfg.xml"));
    return bean.getObject();
    }
    @Bean
    public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }
    @Bean
    public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
    }


    4、MyBatisMapperScannerConfig 配置 

      

    @Configuration
    public class MyBatisMapperScannerConfig {
    @Bean
    public MapperScannerConfigurer primaryMyBatisMapperScannerConfig() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("primarySqlSessionFactory");
    mapperScannerConfigurer.setSqlSessionTemplateBeanName("primarySqlSessionTemplate");
    mapperScannerConfigurer.setBasePackage("com.onecloud.**.mapper.primary");
    buildProperties(mapperScannerConfigurer);
    return mapperScannerConfigurer;
    }
    @Bean
    public MapperScannerConfigurer secondaryMyBatisMapperScannerConfig() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("secondarySqlSessionFactory");
    mapperScannerConfigurer.setSqlSessionTemplateBeanName("secondarySqlSessionTemplate");
    mapperScannerConfigurer.setBasePackage("com.onecloud.**.mapper.secondary");
    buildProperties(mapperScannerConfigurer);
    return mapperScannerConfigurer;
    }

    private void buildProperties(MapperScannerConfigurer mapperScannerConfigurer){
    //初始化扫描器的相关配置,这里我们要创建一个Mapper的父类
    Properties properties = new Properties();
    properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
    properties.setProperty("notEmpty", "false");
    properties.setProperty("IDENTITY", "ORACLE");
    //主键UUID回写方法执行顺序 默认AFTER
    properties.setProperty("ORDER","BEFORE");
    mapperScannerConfigurer.setProperties(properties);
    }



    }

  • 相关阅读:
    数据库查询服务DBCacheServer
    SmallMQ实现发布
    数据库查询服务DBCacheServer
    Mysql ACID与隔离级别
    jsp拾遗
    Git项目创建与提交
    Spring Boot详细学习地址转载
    Spring Cloud微服务体系搭建
    前后端分离项目启动参考
    JVM类加载机制总结
  • 原文地址:https://www.cnblogs.com/enots/p/11112533.html
Copyright © 2020-2023  润新知