栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot+mybatis配置双数据源配置案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot+mybatis配置双数据源配置案例

任务要求

公司某项目需要配置双数据源来调用 sql server 数据库存过,处理存过返回数据并将结果返回到前台页面。

案例实现

因为要调用的是 sql server ,因此我们需要引入对应的驱动依赖。

pom.xml
      
        
            com.microsoft.sqlserver
            mssql-jdbc
            runtime
            6.4.0.jre8
        
	
        
            com.microsoft.sqlserver
            sqljdbc4
            4.0
        
yaml配置
spring:
  application:
    name: spring-boot
  datasource:
    oracle:
      type: org.apache.tomcat.jdbc.pool.DataSource #tomcat连接池,支持druid dbcp dbcp2
      driver-class-name: oracle.jdbc.driver.OracleDriver
      username: username
      password: password 
      url: jdbc:oracle:xxxxxxxxx
    sqlserver:
      type: javax.sql.DataSource
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      username: username
      password: 'test@test:' # 密码有特殊符号时,用单引号即可
      url: jdbc:sqlserver://xxxxxxx

## mapper配置
mybatis:
  mapper-locations: classpath:com/x/xx/*
@Configuration
@MapperScan(basePackages = "com.x.xx.xxx.*.mapper", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class OracleDataSourceConfig {
    
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    
    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    
    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
sql server数据源配置文件
package com.x.xx.smart.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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
// 这里因为是单一需求,所以暂时只需要精准指定mapper路径即可
@MapperScan(basePackages = "com.x.xx.smart.mapper", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class SqlServerDataSourceConfig {

    @Bean(name = "db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.sqlserver")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "db2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
启动文件
import com.x.xx.xxx.common.util.SpringContextUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@EnableScheduling
@ComponentScan("com.x.xx.*") // 因为sql server要和主业务区分开,不能放在xxx包路径下,又懒得动启动类位置了,故而这里需要重新指定一下扫描路径
public class ServiceApplication {

    public static void main(String[] args) {
        ApplicationContext run =SpringApplication.run(ServiceApplication.class, args);
        SpringContextUtil.setApplicationContext(run);
    }

}

附一下调用sql server 存过的写法





    

最后controller,service等按照逻辑实现即可。

参考文档1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671597.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号