springboot+mybatis+pgsql多数据源配置

jdk环境:1.8 配置了双数据源 pgsql+pgsql

 

一、application.yml文件

spring:
  datasource:
    primary:
      jdbc-url: jdbc:postgresql://ip:端口/库
      username: 用户名
      password: 密码
      driver-class-name: org.postgresql.Driver
    secondary:
      dbc-url: jdbc:postgresql://ip:端口/库
      username: 用户名
      password: 密码
      driver-class-name: org.postgresql.Driver

二、创建两个数据源的配置文件

第一个配置文件 :PrimaryDataSourceConfig

参数详情 :@Primary //指定你主要的数据源是哪一个

例如:我这里主要数据源是第一个配置文件 所以我的第二个配置文件并没有加这个注解

注意修改:

  1. @MapperScan里面的basePackages

  2. @ConfigurationProperties里面的prefix 这个是对应yml文件的spring.datasource.primary

  3. 修改第38行的setMapperLocations 后面填写的值就是你xml文件的位置

  4. 修改第39行的setTypeAliasesPackage 值就是 你的实体类的位置

package com.example.rating.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.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 javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.example.rating.mapper",sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDataSourceConfig {

    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    @Primary //指定你主要的数据源是哪一个
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(
            @Qualifier("primaryDataSource") DataSource dataSource)
            throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 设置xml文件存放位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
        bean.setTypeAliasesPackage("com.example.rating.entity");
        return bean.getObject();
    }

    @Bean("primaryTransactionManager")
    @Primary
    public DataSourceTransactionManager primaryTransactionManager(
            @Qualifier("primaryDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

第二个配置文件 :SecondaryDataSourceConfig

修改事项跟第一个一致

package com.example.rating.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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.example.rating.mapper2",sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryDataSourceConfig {

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

    @Bean("secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(
            @Qualifier("secondaryDataSource") DataSource dataSource)
            throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 设置xml文件存放位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper2/*.xml"));
        bean.setTypeAliasesPackage("com.example.rating.entity.db2");
        return bean.getObject();
    }

    @Bean("secondaryTransactionManager")
    public DataSourceTransactionManager secondaryTransactionManager(
            @Qualifier("secondaryDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("secondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

如何使用

正常使用即可

整体结构

springboot+mybatis+pgsql多数据源配置_第1张图片

 

另外如果遇到invalid bound statement (not found)异常可以参考一下: https://blog.csdn.net/Z__Sheng/article/details/93485347

 有什么错误的地方还请指点一下

你可能感兴趣的:(spring,boot,java)