springboot 双数据源配置

发布时间:2023年12月31日

1:pom


        <!--SpringBoot启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!--集成mysql数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.6.1</version>
        </dependency>

        <!--spring boot集成mybatis的依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>


        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.23</version>
        </dependency>

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:目录

文章来源:https://blog.csdn.net/qq_36577291/article/details/135313755
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。