1. 程式人生 > >springboot2.0雙資料來源配置

springboot2.0雙資料來源配置

題記:由於專案中不只是用一個數據庫,所以記下以免忘記。

1、首先展示目錄結構

 

2、pom配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
> <modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId> <artifactId>commodity</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>commodity</name> <description>Demo project for Spring Boot</
description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </
parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

 

3、配置資料來源MasterDataSourceConfiguration.java 和 SlaverDataSourceConfiguration.java

package com.cn.commodity.config;

import com.alibaba.druid.pool.DruidDataSource;
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.beans.factory.annotation.Value;
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
//mapper位置或者dao位置
@MapperScan(basePackages = "com.cn.commodity.dao.accountDao", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterDataSourceConfiguration {

@Value("${spring.datasource.master.driver-class-name}")
private String driverClassName;

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

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

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

@Bean(name = "masterDataSource")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.driverClassName);
dataSource.setUrl(this.url);
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
return dataSource;
}

@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//xml檔案位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mainmapper/*.xml"));
return bean.getObject();
}

@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}

}
package com.cn.commodity.config;

import com.alibaba.druid.pool.DruidDataSource;
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.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
//mapper位置或者dao位置 @MapperScan(basePackages
= "com.cn.commodity.dao.userDao", sqlSessionTemplateRef = "slaverSqlSessionTemplate") public class SlaverDataSourceConfiguration { @Value("${spring.datasource.slaver.driver-class-name}") private String driverClassName; @Value("${spring.datasource.slaver.url}") private String url; @Value("${spring.datasource.slaver.username}") private String username; @Value("${spring.datasource.slaver.password}") private String password; @Bean(name = "slaverDataSource") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(this.driverClassName); dataSource.setUrl(this.url); dataSource.setUsername(this.username); dataSource.setPassword(this.password); return dataSource; } @Bean(name = "slaverSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("slaverDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource);
    //xml位置 bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:adminmapper/*.xml")); return bean.getObject(); } @Bean(name = "slaverTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("slaverDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "slaverSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }

 

4、application.properties配置檔案

#公共配置與profiles選擇無關
mybatis:
typeAliasesPackage:com.cn.commodity.entity
mapperLocations:classpath:adminmapper/*.xml,mainmapper/*.xml


spring:
datasource:
        master:
            spring.datasource.master.driver-class-name= com.mysql.jdbc.Driver
            spring.datasource.master.url=jdbc:mysql://127.0.0.1:3306/test
            spring.datasource.master.username=root
            spring.datasource.master.password=yang156122

        slaver:
            spring.datasource.slaver.driver-class-name= com.mysql.jdbc.Driver
            spring.datasource.slaver.url=jdbc:mysql://127.0.0.1:3306/test1
            spring.datasource.slaver.username=root
            spring.datasource.slaver.password=yang156122

##########互勉###########

有問題可以留言,親測無誤!