蟑螂恶霸的博客 蟑螂恶霸的博客
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

蟑螂恶霸

一个爱折腾的程序员,什么都想试试!
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 常用工具类

    • Session登录失败限制 & RSA密码加密
    • java配置双数据库
      • 原生jdbc工具类
      • orcal下划线转驼峰
      • RSA加密工具类
      • 实现定时任务数据库配置
    • 框架知识

    • 基础技巧

    • java知识集锦
    • 常用工具类
    蟑螂恶霸
    2022-10-15
    目录

    java配置双数据库

    如果你的项目需要两个数据库的话,你可以这么配置你的项目

    # mysqlConfig配置

    /**
     * @author wby
     * @date 2022/4/22
     */
    @Configuration
    @MapperScan(basePackages = "com.example.yiwu.mysqlMapper", sqlSessionTemplateRef  = "mysqlSqlSessionTemplate")
    public class MysqlDataSourceConfig {
    
        @Bean(name = "mysqlDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.mysql")
        @Primary
        public DataSource mysqlDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "mysqlSqlSessionFactory")
        @Primary
        public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
            final MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/yiwu/mysqlMapper/xml/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "mysqlTransactionManager")
        @Primary
        public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "mysqlSqlSessionTemplate")
        @Primary
        public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    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

    # oracleConfig配置

    /**
     * @author wby
     * @date 2022/4/22
     */
    @Configuration
    @MapperScan(basePackages = "com.example.yiwu.oracleMapper", sqlSessionTemplateRef  = "oracleSqlSessionTemplate")
    public class OracleDataSourceConfig {
    
        @Bean(name = "oracleDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.oracle")
        public DataSource oracleDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "oracleSqlSessionFactory")
        public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
            final MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/yiwu/oracleMapper/xml/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "oracleTransactionManager")
        public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "oracleSqlSessionTemplate")
        public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    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

    # yml文件配置

    spring:
      datasource:
        mysql:
          #数据库用户名
          username: root
          #数据库用户密码
          password: root
          #serverTimezone=UTC 解决市区的报错 一般mysql是8.0以上的是必须配置这个
          #userUnicode=true&characterEncoding=utf-8 指定字符编码、解码格式
          jdbc-url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8
          #设置驱动类
          driver-class-name: com.mysql.cj.jdbc.Driver
          #设置数据源
          type: com.alibaba.druid.pool.DruidDataSource
    
          #连接池的配置信息
          ## 初始化大小,最小,最大
          initialPoolSize: 5
          minPoolSize: 5
          maxPoolSize: 20
          maxIdleTime: 120
          acquireIncrement: 2
          idleConnectionTestPeriod: 60
        oracle:
          driver-class-name: oracle.jdbc.OracleDriver
          jdbc-url: jdbc:oracle:thin:@localhost:1521:test
          username: root
          password: root
          type: com.alibaba.druid.pool.DruidDataSource
    
          #连接池的配置信息
          ## 初始化大小,最小,最大
          initialPoolSize: 5
          minPoolSize: 5
          maxPoolSize: 20
          maxIdleTime: 120
          acquireIncrement: 2
          idleConnectionTestPeriod: 60
    
    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
    上次更新: 2022/10/15, 17:42:58
    Session登录失败限制 & RSA密码加密
    原生jdbc工具类

    ← Session登录失败限制 & RSA密码加密 原生jdbc工具类→

    最近更新
    01
    实现定时任务数据库配置
    06-09
    02
    SQL Server连接
    02-22
    03
    RSA加密工具类
    02-22
    更多文章>
    Theme by Vdoing | Copyright © 2022-2023 蟑螂恶霸 | Blog
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式