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

SpringBoot相关系列:多数据源配置

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

SpringBoot相关系列:多数据源配置

阿咚的多数据实现是使用配置类,以及yml文件的方式,ORM使用mybatis 话不多说直接上代码。

数据源1配置类

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 tk.mybatis.spring.annotation.MapperScan;
数据源1

@Configuration
 @MapperScan(basePackages = "com.**.mapper.aDong1",sqlSessionFactoryRef = "aDong1SqlSessionFactory")
 public class aDong1DataSourceConfig {

   @Primary //默认数据源
   @Bean("DaibaiDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.dynamic.aDong1") //yml配置文件
   public DataSource getaDong1DataSource(){
     return DataSourceBuilder.*create*().build();
   }

   @Primary
   @Bean("aDong1SqlSessionFactory")
   public SqlSessionFactory aDong1SqlSessionFactory(@Qualifier("DaibaiDataSource") DataSource dataSource) throws Exception {
     SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
     bean.setDataSource(dataSource);
     // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
     bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong1*.xml"));
     return bean.getObject();
   }


   @Primary
   @Bean("aDong1SqlSessionTemplate")
   public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("aDong1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
     return new SqlSessionTemplate(sqlSessionFactory);
   }
 }

数据源2

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 tk.mybatis.spring.annotation.MapperScan; 
//数据源2

@Configuration
 @MapperScan(basePackages = "com.*.mapper.sso",sqlSessionFactoryRef = "ssoSqlSessionFactory")
 public class aDong2SourceConfig {

   @Primary //默认数据源
   @Bean("aDong2Source")
   @ConfigurationProperties(prefix = "spring.datasource.dynamic.sso") //yml配置文件
   public DataSource getaDong1DataSource(){
     return DataSourceBuilder.*create*().build();
   }

   @Primary
   @Bean("aDong2SqlSessionFactory")
   public SqlSessionFactory aDong2SqlSessionFactory(@Qualifier("aDong2Source") DataSource dataSource) throws Exception {
     SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
     bean.setDataSource(dataSource);
     // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
     bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong2*.xml"));
     return bean.getObject();
   }
   @Bean("aDong2SqlSessionTemplate")
   public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("aDong2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
     return new SqlSessionTemplate(sqlSessionFactory);
   }
 }

yml文件配置: 根据自己情况填上 信息

datasource:
  dynamic:
   primary: dabai *#**设置默认数据源
*   dabai:
    jdbc-url: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 
    password: 
 
   sso:
    jdbc-url: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 
    password: 

使用的时候跟单数据源一样,只要直接使用对应表的mapper就直接可以。

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

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

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