SpringBoot+MybatisPlus配置多数据库

一个数据迁移的项目需要将oracle的数据迁移到达梦数据库,所以需要配置两个数据库,经过网上查阅相关资料,记录一下。

添加maven项目依赖

点击查看代码
  <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>
<!--  oracle驱动包-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
        <!-- 达梦驱动包-->
        <dependency>
            <groupId>com.dm</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>1.8</version>
        </dependency>

配置文件添加数据库信息

点击查看代码
spring:
  datasource:
    oracle:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521/helowin
      username: test
      password: test
    dm:
      driver-class-name: dm.jdbc.driver.DmDriver
      jdbc-url: jdbc:dm://192.168.0.119:5236/MAIN?useUnicode=true&characterEncoding=utf-8
      username: SYSDBA
      password: SYSDBA

添加DataSourceDmConfig配置类

点击查看代码

/**达梦数据配置
 * @author Sin
 * @date 2022/3/8 0008 13:59
 */
@Slf4j
@Configuration
@MapperScan(value = "com.kys.dt.mapper.dm", sqlSessionTemplateRef = "dmSqlSessionTemplate")
public class DataSourceDmConfig {
    @Bean(name = "dmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dm")
    public DataSource dmDataSource() {
        log.info("初始化dm数据源");
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean(name = "dmSqlSessionFactory")
    public SqlSessionFactory dmSqlSessionFactory(@Qualifier("dmDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(org.apache.ibatis.logging.slf4j.Slf4jImpl.class); //设置sql打印
        bean.setDataSource(dataSource);
        bean.setConfiguration(configuration);
        //多数据源配置实体路径
        bean.setTypeAliasesPackage("com.kys.dt.domain.dm");
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources("classpath*:mapper/dm/*.xml"));
        log.info("------dataSource dm 配置成功--------");
        return bean.getObject();
    }

    @Bean(name = "dmTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("dmDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "dmSqlSessionTemplate")
    public SqlSessionTemplate dmSqlSessionTemplate(@Qualifier("dmSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

添加DataSourceOracleConfig配置类

点击查看代码
/**
 * @author Sin   oracle数据库配置
 * @date 2022/3/8 0008 13:58
 */
@Slf4j
@Configuration
@MapperScan(value = "com.kys.dt.mapper.oracle", sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class DataSourceOracleConfig {

    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    @Primary
    public DataSource oracleDataSource() {
        log.info("初始化oracle数据源");
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean(name = "oracleSqlSessionFactory")
    @Primary
    public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);//去掉banner
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(org.apache.ibatis.logging.slf4j.Slf4jImpl.class); //设置sql打印
        bean.setDataSource(dataSource);
        bean.setConfiguration(configuration);
        bean.setGlobalConfig(globalConfig);
        //多数据源配置实体路径
        bean.setTypeAliasesPackage("com.kys.dt.domain.oracle");
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResources("classpath*:mapper/oracle/*.xml"));
        log.info("------dataSource oracle 配置成功--------");
        return bean.getObject();
    }

    @Bean(name = "oracleTransactionManager")
    @Primary
    public PlatformTransactionManager transactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "oracleSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

项目结构及测试结果

posted @   ——椒盐皮皮虾  阅读(594)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示