天天看点

springboot双数据源配置

一个项目里面有多个数据源,配置如下

import javax.sql.DataSource;

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;

@Configuration

public class DataSourceConfig {

    @Bean(name = "db2")

    @Primary

    @ConfigurationProperties(prefix = "spring.datasource.first") // application.properteis中对应属性的前缀

    public DataSource dataSource1() {

        return DataSourceBuilder.create().build();

    }

    @Bean(name = "mysql")

    @ConfigurationProperties(prefix = "spring.datasource.second") // application.properteis中对应属性的前缀

    public DataSource dataSource2() {

        return DataSourceBuilder.create().build();

    }

}

首先创建数据源,数据库位置信息

其次配置双数据源所需要扫描的实体类以及xml的配置信息

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.SqlSessionTemplate;

import org.mybatis.spring.annotation.MapperScan;

import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration

@MapperScan(basePackages = { "com.pactera.vds.member.mapper.second" }, sqlSessionFactoryRef = "sqlSessionFactory1")

public class MybatisDbAConfig {

    @Autowired

    @Qualifier("db2")

    private DataSource db2;

    @Bean

    public SqlSessionFactory sqlSessionFactory1() throws Exception {

        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

        factoryBean.setDataSource(db2); // 使用titan数据源, 连接titan库

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        factoryBean.setVfs(SpringBootVFS.class); // 必须这么样设置,否则,不能识别别名

        factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");

        factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/second/*.xml"));

        return factoryBean.getObject();

    }

    @Bean

    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {

        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());

        // 使用上面配置的Factory

        return template;

    }

}

修改信息basePackages = “” 后面的值,标识着mapper位置, factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");对应着实体对象的包名, factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/second/*.xml"));对应这着xml的配置信息,第二个数据源配置同理

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.SqlSessionTemplate;

import org.mybatis.spring.annotation.MapperScan;

import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration

@MapperScan(basePackages = { "com.pactera.vds.member.mapper" }, sqlSessionFactoryRef = "sqlSessionFactory2")

public class MybatisDbBConfig {

    @Autowired

    @Qualifier("mysql")

    private DataSource mysql;

    @Bean

    public SqlSessionFactory sqlSessionFactory2() throws Exception {

        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

        factoryBean.setDataSource(mysql);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        factoryBean.setVfs(SpringBootVFS.class); // 必须这么样设置,否则,不能识别别名

        factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");

        factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

        return factoryBean.getObject();

    }

    @Bean

    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {

        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());

        return template;

    }

}

创建两个包的位置 : com.pactera.vds.member.mapper;com.pactera.vds.member.mapper.second 

其次创建xml文件位置 : /member/src/main/resources/mybatis   ;/member/src/main/resources/mybatis/second

最后在配置信息上写一下数据源的信息

spring:

  session:

    store-type: none

  datasource:

    first:  

      url: xxxxxxxxxx

      username: xxxxxxxxxx

      password: xxxxxxxxxx

      driver-class-name: com.mysql.jdbc.Driver

    second: 

      url: xxxxxxxxxx

      username: xxxxxxxxxx

      password: xxxxxxxxxx

      driver-class-name: com.mysql.jdbc.Driver 

driver-class-name 标识驱动

以上就完成了双数据源的信息配置,

package com.pactera.vds.member.mapper;

包里面写文件读取的是数据库mysql 这个数据源

继续阅读