springboot 双数据源配置

1:pom


        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            4.13.2
            test
        
        
        
            mysql
            mysql-connector-java
            8.0.26
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
            2.6.1
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
        
            com.alibaba
            druid
            1.1.23
        


        
        
            com.alibaba
            fastjson
            2.0.23
        

2:yml 配置

spring:
  application:
    name: activiti-workflow
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/item-center
    url.activiti: jdbc:mysql://127.0.0.1:3306/activiti
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

3:DBConfig


package com.ikeeper.config;

import com.alibaba.druid.pool.DruidDataSource;
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.beans.factory.annotation.Value;
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  = DBConfig.PACKAGE , sqlSessionFactoryRef = "itemcenterSqlSessionFactory")
public class DBConfig {
    // 精确到 itemcenter 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.ikeeper.mapper.itemcenter";
    private static final String MAPPER_LOCATION = "classpath*:mybatis-mapper/itemcenter/*.xml";
    private static final String DOMAIN_PACKAGE = "com.ikeeper.entity";

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

//    @Value("${spring.datasource.driverClassName}")
//    private String driverClassName;



    @Bean(name="itemcenterDataSource")   //声明其为Bean实例
    public DataSource itemcenterDataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
//        datasource.setDriverClassName(driverClassName);
        return datasource;
    }

    @Bean(name = "itemcenterTransactionManager")
    public DataSourceTransactionManager itemcenterTransactionManager() {
        return new DataSourceTransactionManager(itemcenterDataSource());
    }

    @Bean(name = "itemcenterSqlSessionFactory")
    public SqlSessionFactory itemcenterSqlSessionFactory(@Qualifier("itemcenterDataSource") DataSource itemcenterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(itemcenterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MAPPER_LOCATION));
        sessionFactory.setTypeAliasesPackage(DOMAIN_PACKAGE);
        //mybatis 数据库字段与实体类属性驼峰映射配置
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }




}

package com.ikeeper.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.spring.SpringProcessEngineConfiguration;
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.beans.factory.annotation.Value;
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  = ActivitiDBConfig.PACKAGE , sqlSessionFactoryRef = "activitiSqlSessionFactory")
public class ActivitiDBConfig {
    // 精确到 activiti 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.ikeeper.mapper.activiti";
    private static final String MAPPER_LOCATION = "classpath*:mybatis-mapper/activiti/*.xml";
    private static final String DOMAIN_PACKAGE = "com.ikeeper.entity";

    @Value("${spring.datasource.url.activiti}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

//    @Value("${spring.datasource.driverClassName}")
//    private String driverClassName;



    @Bean(name="activitiDataSource")   //声明其为Bean实例
    public DataSource activitiDataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
//        datasource.setDriverClassName(driverClassName);
        return datasource;
    }

    @Bean(name = "activitiTransactionManager")
    public DataSourceTransactionManager activitiTransactionManager() {
        return new DataSourceTransactionManager(activitiDataSource());
    }

    @Bean(name = "activitiSqlSessionFactory")
    public SqlSessionFactory activitiSqlSessionFactory(@Qualifier("activitiDataSource") DataSource activitiDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(activitiDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MAPPER_LOCATION));
        sessionFactory.setTypeAliasesPackage(DOMAIN_PACKAGE);
        //mybatis 数据库字段与实体类属性驼峰映射配置
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }



    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration(){
        SpringProcessEngineConfiguration standaloneProcessEngineConfiguration = new SpringProcessEngineConfiguration();
        standaloneProcessEngineConfiguration.setDatabaseSchemaUpdate("true");
        standaloneProcessEngineConfiguration.setDataSource(activitiDataSource());
        standaloneProcessEngineConfiguration.setTransactionManager(activitiTransactionManager());
        return standaloneProcessEngineConfiguration;
    }
}

3:目录

springboot 双数据源配置_第1张图片

你可能感兴趣的:(spring,boot,后端,java)