首页 > 数据库 >springboot多数据源整合及使用(一个oracle,两个mysql)

springboot多数据源整合及使用(一个oracle,两个mysql)

时间:2024-08-05 14:39:57浏览次数:10  
标签:sqlSessionFactory springboot 数据源 xxx Bean mysql mybatis new public

  在开发工作中,会遇到需要使用多个数据源的情况,比如项目一开始只有oracle,后面需要追加两个mysql数据源使用,这时候就需要配置多数据源了.

  首先,配置文件的编写:版本如下

spring:
  datasource:
    db1: 
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://xxxxxx:3306/cptdata?characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
      username: xxxxx
      password: xxxx
    db2:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      url: jdbc:oracle:thin:@xxxx:1521:tpsc
      username: xxxx
      password: xxxx
    db3:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://xxxxx:3306/cptetl?characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
      username: xxxxx
      password:xxxxx
    # hikariCP
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      connection-timeout: 60000
      maximum-pool-size: 20
      minimum-idle: 10
      idle-timeout: 600000
      max-lifetime: 1800000
      pool-name: HikariConnectionPool

然后就是编写各自的配置文件:

mysql1:

@Configuration
@MapperScan(basePackages ="xxxx.xxx.xxx.mysql.mapper",sqlSessionFactoryRef="mysqlSqlSessionFactory")
public class mysqlConfig {

    private static final String mapper_location = "classpath:mybatis/mapper/mysql/**/*.xml";
    private static final String mybatis_config_location = "mybatis/mybatis-config.xml";
    private static final String alias_package = xxx.xxx.xxx.xxx";

    @ConfigurationProperties("spring.datasource.db1")
    @Bean
    @Primary
    public DataSourceProperties mysqlDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource mysqlDataSource() {
        return mysqlDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager mysqlTransactionManager() {
        return new DataSourceTransactionManager(mysqlDataSource());
    }

    @Bean
    @Primary
    public SqlSessionFactory mysqlSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(mysqlDataSource());
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
        sqlSessionFactory.setConfigLocation(new ClassPathResource(mybatis_config_location));
        sqlSessionFactory.setTypeAliasesPackage(alias_package);

        sqlSessionFactory.setVfs(SpringBootVFS.class);
        return sqlSessionFactory.getObject();
    }
}

mysql2配置:

@Configuration
@MapperScan(basePackages = "xxx.xxx.xxxx.mysql.etlmapper", sqlSessionFactoryRef = "mysqlEtlSqlSessionFactory")
public class CptMysqlEtlConfig {
    private static final String mapper_location = "classpath:mybatis/mapper/mysqletl/**/*.xml";
    private static final String mybatis_config_location = "mybatis/mybatis-config.xml";
    private static final String alias_package = "xxx.xxxx.xxx.xxx";

    @ConfigurationProperties("spring.datasource.db3")
    @Bean
    @Primary
    public DataSourceProperties mysqlEtlDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource mysqlEtlDataSource() {
        return mysqlEtlDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager mysqlEtlTransactionManager() {
        return new DataSourceTransactionManager(mysqlEtlDataSource());
    }

    @Bean
    @Primary
    public SqlSessionFactory mysqlEtlSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(mysqlEtlDataSource());
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
        sqlSessionFactory.setConfigLocation(new ClassPathResource(mybatis_config_location));
        sqlSessionFactory.setTypeAliasesPackage(alias_package);
        sqlSessionFactory.setVfs(SpringBootVFS.class);
        return sqlSessionFactory.getObject();
    }
}

oracle配置:

@Configuration
@MapperScan(basePackages ="xxx.xxx.xxx.oracle.mapper",sqlSessionFactoryRef="oracleSqlSessionFactory")
public class CptOracleConfig {

    private static final String mapper_location = "classpath:mybatis/mapper/oracle/**/*.xml";
    private static final String mybatis_config_location = "mybatis/mybatis-config.xml";
    private static final String alias_package = "xxx.xxx.xxx.xxx";

    @ConfigurationProperties("spring.datasource.db2")
    @Bean
    public DataSourceProperties oracleDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource oracleDataSource() {
        return oracleDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    public DataSourceTransactionManager cptOracleTransactionManager() {
        return new DataSourceTransactionManager(oracleDataSource());
    }


    @Bean
    public JdbcTemplate oracleJdbcTemplate(@Qualifier("oracleDataSource") DataSource oracleDataSource) {
        return new JdbcTemplate(oracleDataSource);
    }

    @Bean
    public SqlSessionFactory oracleSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(oracleDataSource());
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
        sqlSessionFactory.setConfigLocation(new ClassPathResource(mybatis_config_location));
        sqlSessionFactory.setTypeAliasesPackage(alias_package);

        sqlSessionFactory.setVfs(SpringBootVFS.class);
        return sqlSessionFactory.getObject();
    }
}

mybatis-config.xml如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="false"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="10"/>
    </settings>
</configuration>

之后在配置的目录里编写mapper查询接口和在配置的资源目录编写mapper.xml文件即可.

 

标签:sqlSessionFactory,springboot,数据源,xxx,Bean,mysql,mybatis,new,public
From: https://www.cnblogs.com/wnhbx/p/18343167

相关文章

  • 系统整容纪:用知识来"武装"自己~认识MySQL的锁与事务
    本文通过介绍在实际工作中一次异常排查引发的自我思考与学习,来使得读者受到一定的启发,从而迸发出星星点光,扩展出自己独有的思路,进而在工作中不断的挖掘自我不足之处,同时通过学习与"锻炼"来不断地强大自己。分享工作中的点点滴滴,贯彻千里之行,始于足下,最终以微不足道的量变引起化蝶......
  • 免费【2024】springboot 大学校园旧物捐赠网站的设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • 免费【2024】springboot 大学生家教管理系统的设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • springboot+vue酒店信息管理系统【程序+论文+开题】-计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着旅游业的蓬勃发展和消费者需求的日益多元化,酒店行业正面临着前所未有的挑战与机遇。传统的酒店管理模式已难以满足现代酒店高效运营、精准服务及客户体验优化的需求。在此背景下,酒店信息管理系统(HotelInformationManagementSys......
  • springboot+vue酒店信息管理系统【程序+论文+开题】-计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着旅游业的蓬勃发展,酒店业作为旅游产业链中的重要一环,面临着日益增长的客户需求与激烈的市场竞争。传统的手工管理模式已难以满足现代酒店对高效、精准、个性化服务的需求。在此背景下,开发一套集用户管理、房间类别划分、客房信息展......
  • springboot+vue酒店客房预订系统【程序+论文+开题】-计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着旅游业的蓬勃发展,酒店行业作为旅游业的重要支柱,面临着日益增长的客户需求和激烈的市场竞争。传统的酒店预订方式,如电话预订或到店预订,已难以满足现代消费者对于便捷性、实时性和个性化服务的高要求。在此背景下,开发一套高效、智能......
  • springboot+vue酒店客房管理系统的设计与实现【程序+论文+开题】-计算机毕业设计
    系统程序文件列表开题报告内容研究背景随着旅游业的蓬勃发展,酒店业作为其核心组成部分,面临着日益增长的客户需求与管理复杂性的双重挑战。传统的手工酒店客房管理方式已难以满足现代酒店高效、精准、便捷的管理需求。宾客对住宿体验的要求不断提高,期望通过数字化手段实现快......
  • 华为欧拉系统离线安装MySQL5.7步骤
    一、需要准备的软件1、mysql官网下载地址:https://dev.mysql.com/downloads/mysql/下载mysql-5.7.24-linux-glibc2.12-x86_64.tar二、下面开始部署安装mysql1、创建新的用户组和新的用户,用来管理mysql,提高安全性#创建新数组mysqlgroupaddmysql#创建用户mysql,指......
  • SpringBoot-书店信息管理系统+93494(免费领源码+开发文档)可做计算机毕业设计JAVA、PHP
    基于springboot书店信息管理系统摘 要书店信息管理系统采用B/S结构、java开发语言、以及Mysql数据库等技术。系统主要分为管理员和用户两部分,管理员管理主要功能包括:首页、轮播图、公告栏、资源管理(图书资讯、资讯分类)交流管理(留言板、留言板分类)系统用户(管理员、顾客用户......
  • springboot爱宠屋宠物商店管理系统-计算机毕业设计源码52726
    目录摘要1绪论1.1选题背景与意义1.2国内外研究现状1.3论文结构与章节安排2系统分析2.1可行性分析2.2系统流程分析2.2.1系统开发流程2.2.2用户登录流程2.2.3系统操作流程2.2.4添加信息流程2.2.5修改信息流程2.2.6删除信息流程2.3 系统......