springboot multiple data sources

Project demo GitHub address

This article is for children's shoes who have mastered mybatis to configure a single database . For those who have not mastered mybatis to configure a single data source, you can find other articles to make up for it

Both tests can be performed.
insert image description here

Integrate mybatis

Refer to the multi-data source configuration of blog
mybatis

  1. Create [entity], [interface class], [mapper file], [database]. Note that the name of the interface class cannot be the same
  2. Write the yrm file, configure [data source information], pay attention to the use of jdbcurl, not url
  3. Write the config class to inject [Data Source], [SqlSessionFactory], and configure the location information of [Interface File] and [Mapping File].
spring:
  datasource:
    bookstore:
      jdbcurl: jdbc:mysql://localhost:3306/bookstore2?serverTimezone=UTC    # 这个是jdbcurl不是url。
      username: root
      password: 1230
      driver-class-name: com.mysql.cj.jdbc.Driver
    test:
      jdbcurl: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
      username: root
      password: 1230
      driver-class-name: com.mysql.cj.jdbc.Driver

  • @MapperScan is used to specify [interface class], [SqlSession] information
  • Then create the data source
  • Create a SqlSession factory and configure the location of the mapping file. Note the use of this getResources method. Not getResource.
  • Create a transaction management factory
package com.springboot.config;

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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resources;
import javax.sql.DataSource;

/**
 * @author: Zekun Fu
 * @date: 2023/4/10 17:59
 * @Description: 第一个数据源的配置
 */
@Configuration
// 指定接口包,数据源
@MapperScan(basePackages = "com.springboot.dto.bookstore", sqlSessionFactoryRef = "bookStoreSqlSessionFactory")
public class BookStoreDataSourceConfig {
    
    
    // 指定扫描文件
    private static final String MAPPER_LOCATION = "classpath*:mapper/bookStore/*.xml";


    @Primary
    @Bean(name = "bookStoreDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.bookstore")
    public DataSource dataSource() {
    
    
        return DataSourceBuilder.create().build();
    }


    /**
     * 事务管理器
     */
    @Bean(name = "bookStoreTransactionManager")
    public PlatformTransactionManager dataSourceTransactionManager(@Qualifier("bookStoreDataSource") DataSource dataSource) {
    
    
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "bookStoreSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("bookStoreDataSource") DataSource dataSource) throws Exception{
    
    
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(BookStoreDataSourceConfig.MAPPER_LOCATION);
        sessionFactoryBean.setMapperLocations(resources);
        return sessionFactoryBean.getObject();
    }
}

Guess you like

Origin blog.csdn.net/fuzekun/article/details/130068555