• Java mysql 双数据源


    场景

    在开发过程中我们常常会用到两个数据库,一个数据用来实现一些常规的增删改查,另外一个数据库用来实时存数据。进行数据的统计分析。可以读写分离。可以更好的优化和提高效率;
    或者两个数据存在业务分离的时候也需要多个数据源来实现

    使用方式

    1、application.properties 中 数据库配置

    #数据库配置
    spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/gds?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false 
    spring.datasource.db1.username=root
    spring.datasource.db1.password=root
    spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
    
    spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/zkhx?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
    spring.datasource.db2.username=root
    spring.datasource.db2.password=root
    spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    config文件配置

    1、配置 spring.datasource.db1

    注:basePackages = “com.zkhx.dao.master”:
    prefix = “spring.datasource.db1”
    绑定master目录下使用的是数据库db1 也就是gds

    package com.zkhx.config;
    
    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 javax.sql.DataSource;
    
    
    @Configuration
    // 配置mybatis的接口类放的地方
    @MapperScan(basePackages = "com.zkhx.dao.master", sqlSessionFactoryRef = "dbSqlSessionFactory")
    public class DataSourceConfig {
    
        @Bean(name = "db")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.db1")
        public DataSource getDateSource1() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        @ConfigurationProperties(prefix = "mybatis.configuration")
        public org.apache.ibatis.session.Configuration configuration() {
            return new org.apache.ibatis.session.Configuration();
        }
    
    
        @Bean(name = "dbSqlSessionFactory")
        @Primary
        public SqlSessionFactory test1SqlSessionFactory(@Qualifier("db") DataSource datasource, org.apache.ibatis.session.Configuration configuration)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(datasource);
            bean.setConfiguration(configuration);
            bean.setMapperLocations(
                    // 设置mybatis的xml所在位置
                    new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/master/*.xml"));
            return bean.getObject();
        }
        @Bean("dbSqlSessionTemplate")
        // 表示这个数据源是默认数据源
        @Primary
        public SqlSessionTemplate test1sqlsessiontemplate(
                @Qualifier("dbSqlSessionFactory") SqlSessionFactory sessionfactory) {
            return new SqlSessionTemplate(sessionfactory);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    2、配置 spring.datasource.db2

    package com.zkhx.config;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Configuration;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    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.core.io.support.PathMatchingResourcePatternResolver;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.zkhx.dao.vice", sqlSessionFactoryRef = "db2SqlSessionFactory")
    public class DataSourceViceConfig {
        @Bean(name = "db2")
        @ConfigurationProperties(prefix = "spring.datasource.db2")
        public DataSource getDateSource2() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "db2SqlSessionFactory")
        public SqlSessionFactory test2SqlSessionFactory(@Qualifier("db2") DataSource datasource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(datasource);
            bean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/vice/*.xml"));
            return bean.getObject();
        }
        @Bean("db2SqlSessionTemplate")
        public SqlSessionTemplate test2sqlsessiontemplate(
                @Qualifier("db2SqlSessionFactory") SqlSessionFactory sessionfactory) {
            return new SqlSessionTemplate(sessionfactory);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    3、截图

    在这里插入图片描述
    在这里插入图片描述

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zkhx.dao.vice.StDBDataDao">
    	 
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    论文-分布式-并发控制-Lamport逻辑时钟
    el-table 动态合并单元格和给某一行添加颜色
    09-基础篇-一步一步写MakeFile
    共享模型之管程
    循环神经网络-简洁实现
    python 操作jira
    LintCode 3208:String splitting字符串处理入门题
    Mars3d-vue最简项目模板集成使用Mars3d的UI控件样板
    湖北省科技企业孵化器和众创空间申报奖励补贴标准,2022年认定条件汇总
    P1950 长方形
  • 原文地址:https://blog.csdn.net/qq_36628003/article/details/125480146