mybatis基于注解形式的多数据源

最近在做一个系统管理项目,需要使用到多数据源,尝试了注解形式和xml形式的多数据源配置,以下是基于注解形式的Mybatis多数据源配置。

1.application.yml 配置文件

database-einstein:
  driver-class-name: org.postgresql.Driver
  url: jdbc:postgresql://192.168.128.129:5432/einstein
  username: postgres
  password: 123456
  config-location: mybatis-web-config.xml


database-dataplatform:
  driver-class-name: org.postgresql.Driver
  url: jdbc:postgresql://192.168.128.129:5432/DataPlatform_20180107
  username: postgres
  password: postgres
  config-location: mybatis-web-config.xml


server:
  port: 8093

2.针对einstein数据库的配置类(配置多数据源一定要有主数据源,如类中所示,可以在主数据源的sessionFactory上加上注解@Primary

package cn.antiy.weiqing.configutation;

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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author miaoying
 * @date 2/5/18
 */
@Configuration
@MapperScan(basePackages = {"cn.antiy.weiqing.mapper.einstein"}, sqlSessionFactoryRef = "einsteinSqlSessionFactory")
public class MybatisEinsteinDbConfig {
    @Bean
    @ConfigurationProperties(prefix = "database-einstein")
    public DataSource dataSourceEinstein() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory einsteinSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceEinstein());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate einsteinSqlSessionTemplate() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(einsteinSqlSessionFactory());
        return template;
    }
}

3.针对DataPlatform_20180107数据库的配置类

package cn.antiy.weiqing.configutation;

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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author miaoying
 * @date 2/5/18
 */
@Configuration
@MapperScan(basePackages = {"cn.antiy.weiqing.mapper.dataplatform"}, sqlSessionFactoryRef = "dataPlatformSqlSessionFactory")
public class MybatisDataPlatformDbConfig {


    @Bean
    @ConfigurationProperties(prefix = "database-dataplatform")
    public DataSource dataSourceDataPlatform() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory dataPlatformSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceDataPlatform());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate dataPlatformSqlSessionTemplate() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(dataPlatformSqlSessionFactory());
        return template;
    }
}

4.Application.java(由于Spring Boot默认单数据源,所以在启动类中需要使用注解去除该默认项,如下所示:@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

package cn.antiy.weiqing;

import cn.antiy.weiqing.constants.SysConstants;
import cn.antiy.weiqing.utils.SpringContextUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners(new AppStartListener());
        application.addListeners();
        application.run(args);
    }
}

class AppStartListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        SpringContextUtils.setApplicationContext(event.getApplicationContext());
        new SysConstants().init();
    }
}
原文地址:https://www.cnblogs.com/miaoying/p/8432398.html