mybatis多数据源

  • 配置
spring:
  datasource:
    admin:
      username: root
      password: root
      jdbc-url: jdbc:mysql://127.0.0.1:3306/admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=true&connectTimeout=3000&socketTimeout=10000
      driver-class-name: com.mysql.cj.jdbc.Driver
      pool-name: HikariPool-admin
      minimum-idle: 10
      maximum-pool-size: 100
      idle-timeout: 600000
      max-lifetime: 1800000
      connection-timeout: 30000
      type: com.zaxxer.hikari.HikariDataSource
      connection-test-query: SELECT 1
    front:
      username: root
      password: root
      jdbc-url: jdbc:mysql://127.0.0.1:3306/front?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=true&connectTimeout=3000&socketTimeout=10000
      driver-class-name: com.mysql.cj.jdbc.Driver
      pool-name: HikariPool-front
      minimum-idle: 10
      maximum-pool-size: 100
      idle-timeout: 600000
      max-lifetime: 1800000
      connection-timeout: 30000
      type: com.zaxxer.hikari.HikariDataSource
      connection-test-query: SELECT 1
  • datasource1
package com.olive.config;

import com.zaxxer.hikari.HikariDataSource;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@MapperScan(basePackages = {"com.admin.dao.rdb.mapper"}, sqlSessionFactoryRef = "adminSqlSessionFactory")
public class AdminDataSourceConfig {

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

    @Primary
    @Bean("adminDataSourceTransactionManager")
    public DataSourceTransactionManager adminDataSourceTransactionManager(@Qualifier("adminDataSource") DataSource dataSource) {
        HikariDataSource ds = (HikariDataSource)dataSource;
        log.info("adminDataSource MaximumPoolSize={}, MinimumIdle={}", ds.getMaximumPoolSize(), ds.getMinimumIdle());
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean("adminSqlSessionFactory")
    public SqlSessionFactory adminSqlSessionFactory(@Qualifier("adminDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/admin/*Mapper.xml");
        sqlSessionFactory.setMapperLocations(resources);
        return sqlSessionFactory.getObject();
    }

    /**
     * 构建sqlSession
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "adminSqlSessionTemplate")
    public SqlSessionTemplate adminSqlSessionTemplate(@Qualifier("adminSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

  • datasource2
package com.olive.config;

import com.zaxxer.hikari.HikariDataSource;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;

/**
 * @Desc:
 * @Author: huang.jinjin1
 * @Date: 2024/5/7
 */
 @Slf4j
@Configuration
@MapperScan(basePackages = {"com.front.dao.rdb.mapper"}, sqlSessionFactoryRef = "frontSqlSessionFactory")
public class FrontDataSourceConfig {

    @Bean("frontDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.front")
    public DataSource frontDataSource() {
        DataSource dataSource = DataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean("frontDataSourceTransactionManager")
    public DataSourceTransactionManager frontDataSourceTransactionManager(@Qualifier("frontDataSource") DataSource dataSource) {
        HikariDataSource ds = (HikariDataSource)dataSource;
       log.info("frontDataSource MaximumPoolSize={}, MinimumIdle={}", ds.getMaximumPoolSize(), ds.getMinimumIdle());
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("frontSqlSessionFactory")
    public SqlSessionFactory frontSqlSessionFactory(@Qualifier("frontDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/front/*Mapper.xml");
        sqlSessionFactory.setMapperLocations(resources);
        return sqlSessionFactory.getObject();
    }

    /**
     * 构建sqlSession
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "frontSqlSessionTemplate")
    public SqlSessionTemplate frontSqlSessionTemplate(@Qualifier("frontSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值