springboot配置来连接多个mysql数据库

1,在yml里配置多数据库
spring:
  datasource:
    app1:
      jdbc-url: jdbc:mysql://localhost:3306/data1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false
      username: root
      password: 123456
    app2:
      jdbc-url: jdbc:mysql://localhost:3306/data2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false
      username: root
      password: 123456
2,配置数据库配置
(1)第一个数据库配置类DataSource1Config
package com.linzhuo.home.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.Configurable;
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.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author: tt
 * Date: 2023/3/9 上午10:12
 */
@Configurable
@MapperScan(basePackages = {"com.linzhuo.home.dao.app1","com.linzhuo.mbg.mapper"},sqlSessionTemplateRef  = "app1SqlSessionTemplate")
public class DataSource1Config {
    @Bean(name = "app1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.app1")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "app1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("app1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/linzhuo/home/dao/app1/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "app1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("app1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "app1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("app1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

(2)第二个数据库配置类DataSource2Config
package com.linzhuo.home.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;

/**
 * @author: jss
 * Date: 2023/3/9 上午10:12
 */
@Configuration
@MapperScan(basePackages = {"com.linzhuo.home.dao.app2","com.linzhuo.msg.mapper"}, sqlSessionTemplateRef  = "app2SqlSessionTemplate")
public class DataSource2Config {

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

    @Bean(name = "app2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("app2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/linzhuo/home/dao/app2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "app2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("app2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "app2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("app2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

注意点

第二个数据库配置不带@Primary ,@Primary为主数据源,如果两个都带有会报错

3,如果有自己配置的dao

springboot配置来连接多个mysql数据库_第1张图片

4,在MybatisConfig配置类里添加配置
/**
 * MyBatis相关配置
 */
@Configuration
@EnableTransactionManagement
@MapperScan({"com.linzhuo.home.dao","com.linzhuo.mbg.mapper"})
public class MyBatisConfig {
}
5 如果报’url’ attribute is not specified and no embedded datasource could be configured

这个错误,是设置的数据库url没有识别到,看是否两个都加了@Primary,或者在启动类上加下

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

你可能感兴趣的:(spring,boot,mysql,数据库)