美文网首页
springboot mybatis多数据源配置

springboot mybatis多数据源配置

作者: 毛于晏 | 来源:发表于2019-01-12 23:52 被阅读132次

在我们进行开发过程中, 往往会使用不止一个数据库, 可能有很多, 又可能该数据库在另一个服务器上; 那么我们这里就需要在一个项目中配置多个数据库的数据源, 自由的进行数据持久化操作;

demo在此!!! demo在此!!! demo在此!!!
多数据源demo

1.创建一个springboot整合mybatis的项目

备注: 这里用的是mybatis-plus, 用法大致和mybatis一样
如果你还不会整合该项目,请看:
手把手教你springboot快速整合mybatis
如果你会了, 请忽略;

2.多数据源配置

备注: 这里用的是阿里的, druid作为数据源

2.1 yml文件

spring:
  datasource:
  #db1 这个名字可以随便取, 为了注入数据源数据的时候能分辨哪个是哪个
  #第一个数据源, 默认数据源,
    db1:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8
      username: root
      password: root
      #第二个数据源
    db2:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf-8
      username: root
      password: root
      #第三个数据源
    db3:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db3?useUnicode=true&characterEncoding=utf-8
      username: root
      password: root
      #以此类推 .....

2.2 数据源类配置

默认数据源

bean.setTypeAliasesPackage("com.example.springboot.pojo");这里注意, 设置该参数时打成jar启动会找不到该包下的类,目前未找到解决方案

package com.example.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
//指定该SqlSession对象对应的dao(basePackages , dao扫包  sqlSessionFactoryRef: SqlSessionFactory对象注入到该变量中)
@MapperScan(basePackages = "com.example.springboot.DB1Dao", sqlSessionFactoryRef = "DB1Factory")
public class DB1DataSourceConfig {

    /**
     * 封装数据源对象创建, 该方法就已经将数据源的各个数据封装到该对象中
     * @return
     */
    @Bean(name = "DB1dataSource")
    @Primary //必须要有, 说明该数据源是默认数据源
    @ConfigurationProperties(prefix = "spring.datasource.db1") //读取的数据源前缀, 跟yml文件对应
    public DataSource DB1dataSource(){
        return new DruidDataSource();
    }

    /**
     * SqlSession对象创建
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "DB1Factory")
    @Primary
    public SqlSessionFactory DB1Factory(@Qualifier("DB1dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //指定起别名的包, 这里注意, 设置该参数时打成jar启动会找不到该包下的类,目前未找到解决方案
        bean.setTypeAliasesPackage("com.example.springboot.pojo");
        bean.setDataSource(dataSource);
        //指定该SqlSession对应的mapper.xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:DB1Mapper/*.xml"));
        return bean.getObject();
    }

}

其他数据源1

package com.example.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.example.springboot.DB2Dao", sqlSessionFactoryRef = "DB2Factory")
public class DB2DataSourceConfig {

    @Bean(name = "DB2dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource DB1dataSource(){
        return new DruidDataSource();
    }

    @Bean(name = "DB2Factory")
    public SqlSessionFactory DB1Factory(@Qualifier("DB2dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
       //这里注意, 设置该参数时打成jar启动会找不到该包下的类,目前未找到解决方案
        bean.setTypeAliasesPackage("com.example.springboot.pojo");
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:DB2Mapper/*.xml"));
        return bean.getObject();
    }

}

其他数据源2

package com.example.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
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.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.example.springboot.DB3Dao", sqlSessionFactoryRef = "DB3Factory")
public class DB3DataSourceConfig {

    @Bean(name = "DB3dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db3")
    public DataSource DB1dataSource(){
        return new DruidDataSource();
    }

    @Bean(name = "DB3Factory")
    public SqlSessionFactory DB1Factory(@Qualifier("DB3dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
       //这里注意, 设置该参数时打成jar启动会找不到该包下的类,目前未找到解决方案
        bean.setTypeAliasesPackage("com.example.springboot.pojo");
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:DB3Mapper/*.xml"));
        return bean.getObject();
    }

}

其他数据源x
与其他数据源2, 3一样以此类推

2.3 数据源配置类对应的一些目录

多数据源.jpg

相关文章

网友评论

      本文标题:springboot mybatis多数据源配置

      本文链接:https://www.haomeiwen.com/subject/xmyhdqtx.html