首页 > 其他分享 >springboot项目配置多数据源

springboot项目配置多数据源

时间:2024-01-15 19:11:57浏览次数:51  
标签:springboot spring 配置 springframework datasource 数据源 org import sqlSessionFactory

springboot项目配置多数据源

//关键:mybatis文件的目录需要区分开来
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis.myProjectOne/*.xml"));

#从数据库配置,数据库的配置 以spring.datasource.myProjectOne前缀,需要配置驱动程序名称。
spring.datasource.myProjectOne.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.myProjectOne.url = jdbc:mysql://IP:3306/databaseName?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.myProjectOne.username = username
spring.datasource.myProjectOne.password = password

从数据源

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * 数据源配置
 */
@Configuration
@MapperScan(basePackages = "com.my.projectOne.mapper", sqlSessionFactoryRef = "myProjectOneDBSessionFactory")
public class AutoInsureDBConfig {

    @Bean(name = "myProjectOneDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.myProjectOne")
    @Primary
    public DataSource fundsDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "myProjectOneDBSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("myProjectOneDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //关键:mybatis文件的目录需要区分开来
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis.myProjectOne/*.xml"));   //子模块中的类路径 mybatis.myProjectOne
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "myProjectOneTransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("myProjectOneDataSource") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean(name = "myProjectOneTransactionTemplate")
    @Primary
    public TransactionTemplate transactionTemplate(@Qualifier("myProjectOneTransactionManager") DataSourceTransactionManager transactionManager) {
        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        return transactionTemplate;
    }
}
主数据库配置,数据库的配置 以spring.datasource前缀,需要配置驱动程序名称。
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://IP:3306/databaseName?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username = username
spring.datasource.password = password

主数据源

import com.autoyol.mybatis.encrypt.intercept.ResultDecryptSetInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 数据源配置
 */
@Configuration
@MapperScan(basePackages = "com.my.projectTwo.mapper", sqlSessionFactoryRef = "myProjectTwodbSessionFactory")
public class InsurancePurchaseDBConfig {

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


    @Bean(name = "myProjectTwodbSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("myProjectTwodbDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{new ResultDecryptSetInterceptor()});
        return sqlSessionFactoryBean.getObject();
    }
}

 

标签:springboot,spring,配置,springframework,datasource,数据源,org,import,sqlSessionFactory
From: https://www.cnblogs.com/oktokeep/p/17966092

相关文章

  • Idea SpringBoot 子模块 加载不到该子模块根目录config下面的配置文件
    IdeaSpringBoot子模块加载不到该子模块根目录config下面的配置文件importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframew......
  • webpack,babel配置项目适配IE11
    package.json"browserslist":["ie11"] webpack.config.jsconstpath=require('path')consthtmlWebpackPlugin=require('html-webpack-plugin')const{CleanWebpackPlugin}=require('clean-webpack-......
  • springboot + mybatis plus 全局添加查询字段反引号
    配置文件添加: column-format:"`%s`"mybatis-plus:#启动时是否检查MyBatisXML文件是否存在check-config-location:true#MyBatis原生配置configuration:#字段名称下划线转驼峰命名map-underscore-to-camel-case:trueglobal-config:db-co......
  • 12、nginx日志配置
    1.nginx日志指令log_format:用于设置日志格式access_log:用于指定日志文件存放路径、格式、缓存大小可设置在http、server块中2.log_format2.1log_format语法log_formatnameformat[format...]name表示定义的格式名称format表示定义的格式样式。log_forma......
  • 烟火检测/周界入侵/视频智能识别AI智能分析网关V4如何配置ONVIF摄像机接入
    AI边缘计算智能分析网关V4性能高、功耗低、检测速度快,易安装、易维护,硬件内置了近40种AI算法模型,支持对接入的视频图像进行人、车、物、行为等实时检测分析,上报识别结果,并能进行语音告警播放。算法可按需组合、按场景配置,平台支持前端设备管理、实时视频预览、录像与云存储、告警......
  • Windows OhmGraphite 配置
    WindowsOhmGraphite配置由于windows_exporter无法监控温度相关的指标,那么就需要使用OhmGraphite进行监控该指标。下载访问https://github.com/nickbabcock/OhmGraphite/releases/地址进行下载最新的版本,下载后解压到你自己放的目录修改配置编辑OhmGraphite.exe.config文......
  • SpringBoot中整合ElasticSearch实现增删改查等操作
    场景SpringBoot中整合ElasticSearch快速入门以及踩坑记录:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/135599698在上面进行集成的基础上,实现对ES数据的增删改查等操作。注:博客:https://blog.csdn.net/badao_liumang_qizhi实现1、ElastciSearch的对象映射h......
  • prometheus 日常配置记录
    通用模糊匹配irate(node_network_transmit_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*"}[5m])>0kube-prometheus部署配置说明kind:Prometheusmetadata:#spec:alerting:alertmanagers:-name:prometheus-prometheus-oper-alertmanager#定义......
  • 多数据源
    dynamic:primary:master#设置默认的数据源或者数据源组,默认值即为masterstrict:true#严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源druid:#空闲时执行连接测试test-while-idle:true......
  • nginx配置虚拟主机机
    1.配置文件#设置进程数worker_processes4;#设置启动用户,一般情况下设置为当前用户,要不然会出现权限不足userroot;events{worker_connections1024;}http{#设置虚拟主机1server{listen8001;server_name172.16.122.177;......