springboot+mybatis多数据源配置

12 篇文章 0 订阅

目录结构:dao目录下放mapper类,entity目录下放实体类,resources目录下mapper目录放对应xml文件

application.yml配置

spring:
  application:
    name: blog
  datasource:
    blog:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/blog?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      username: root
      password: password
    clock:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/clock?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      username: root
      password: password

数据源blog配置类

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.wgs.blog.dao.blog",sqlSessionTemplateRef = "blogSqlSessionTemplate")
public class BlogDataSourceConfig {
    /**
    * 创建数据源
    * @return DataSource
    */
    @Bean(name = "blogDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.blog")
    @Primary
    public DataSource blogDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
    * 创建工厂
    *@param dataSource
    *@throws Exception
    *@return SqlSessionFactory
    */
    @Bean(name = "blogSqlSessionFactory")
    @Primary
    public SqlSessionFactory blogSqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wgs/blog/mapper/blog/*.xml"));
        return bean.getObject();
    }

    /**
    * 创建事务
    *@param dataSource
    *@return DataSourceTransactionManager
    */
    @Bean(name = "blogTransactionManager")
    @Primary
    public DataSourceTransactionManager blogDataSourceTransactionManager(@Qualifier("blogDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
    * 创建模板
    *@param sqlSessionFactory
    *@return SqlSessionTemplate
    */
    @Bean(name = "blogSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate blogSqlSessionTemplate(@Qualifier("blogSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 数据源clock配置类

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.wgs.blog.dao.clock",sqlSessionTemplateRef = "clockSqlSessionTemplate")
public class ClockDataSourceConfig {
    /**
     * 创建数据源
     * @return DataSource
     */
    @Bean(name = "clockDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.clock")
    public DataSource clockDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 创建工厂
     *@param dataSource
     *@throws Exception
     *@return SqlSessionFactory
     */
    @Bean(name = "clockSqlSessionFactory")
    public SqlSessionFactory clockSqlSessionFactory(@Qualifier("clockDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/wgs/blog/mapper/clock/*.xml"));
        return bean.getObject();
    }

    /**
     * 创建事务
     *@param dataSource
     *@return DataSourceTransactionManager
     */
    @Bean(name = "clockTransactionManager")
    public DataSourceTransactionManager clockDataSourceTransactionManager(@Qualifier("clockDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 创建模板
     *@param sqlSessionFactory
     *@return SqlSessionTemplate
     */
    @Bean(name = "clockSqlSessionTemplate")
    public SqlSessionTemplate clockSqlSessionTemplate(@Qualifier("clockSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值