springboot 集成 mybaits 多数据源

本文介绍了如何在SpringBoot中配置多个数据源,分别展示了天气数据源和客商数据源的配置,包括数据源、SqlSessionFactory、SqlSessionTemplate和事务管理器的创建。同时提到了在配置多数据源时@Primary注解的使用限制,并提供了一个处理静态工具类中注入bean的技巧。
摘要由CSDN通过智能技术生成

application.yml中数据源配置:

#数据库连接  天气数据
spring:  
    datasource:   
            keshangjdbc: 
                   url: jdbc:oracle:thin:@//IP:1565/ods
                   username: 
                   password: 
                 #  driver-class-name: oracle.jdbc.driver.OracleDriver  
#数据库连接 客商数据 
            weatherjdbc: 
                  url: jdbc:oracle:thin:@//IP:1565/ods
                  username: 
                  password: 
                #  driver-class-name: oracle.jdbc.driver.OracleDriver     

对每中链接写一个配置类,主要是通过aop的方式链接注入到SqlSessionTemplate中,

客商的配置类如下:

package com.neusoft.interf.dataSources;

import javax.sql.DataSource;

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 org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @author 
 * @Time:2018年12月19日 上午11:08:35
 * @version 1.0
 */
@Configuration
@MapperScan(basePackages = "com.neusoft.in

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值