springboot2.1.5+mybatis配置多数据源

编程

一、启动是创建bean的方式

pom.xml

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.10</version>

</dependency>

<!-- maven仓库中没有oracle数据库驱动,这里将jar 放到工程下进行引入 -->

<dependency>

<groupId>com.ojdbc</groupId>

<artifactId>ojdbc</artifactId>

<version>10.2.0.1</version>

<scope>system</scope>

<systemPath>${project.basedir}/src/main/lib/ojdbc14-10.2.0.1.0.jar</systemPath>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.38</version>

</dependency>

application.properties

#oracle数据库

spring.datasource.master.url=jdbc:oracle:thin:@localhost:1521:test

spring.datasource.master.username=oracle

spring.datasource.master.password=123456

spring.datasource.master.driverClassName=oracle.jdbc.OracleDriver

spring.datasource.master.initialSize=5

spring.datasource.master.maxActive=15

spring.datasource.master.minIdle=5

spring.datasource.master.poolPreparedStatements=true

spring.datasource.master.validationQuery=SELECT 1 FROM DUAL

#mysql数据库

spring.datasource.slave.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8

spring.datasource.slave.username=mysql

spring.datasource.slave.password=123456

spring.datasource.slave.driverClassName=com.mysql.cj.jdbc.Driver

spring.datasource.slave.initialSize=5

spring.datasource.slave.maxActive=15

spring.datasource.slave.minIdle=5

spring.datasource.slave.poolPreparedStatements=true

spring.datasource.slave.validationQuery=SELECT 1

MasterDataSourceConfig.java

注:Primary注解代表改数据源为主数据源

package com.core.dataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

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.boot.context.properties.ConfigurationProperties;

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;

/**

* oracle数据配置

*/

@Configuration

//配置mapper文件目录,进行扫描

@MapperScan(basePackages = "com.wawj.core.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate")

public class MasterDataSourceConfig {

/**

* 创建数据源

* @return

*/

@Bean

@Primary

@ConfigurationProperties(prefix = "spring.datasource.master")

public DataSource masterDataSource() {

return DruidDataSourceBuilder.create().build();

}

/**

* 创建工厂

* @param dataSource

* @return

* @throws Exception

*/

@Bean

@Primary

public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

//指定mapper.xml的目录

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/**/*.xml"));

//指定实体类目录

bean.setTypeAliasesPackage("com.wawj.core.entity");

return bean.getObject();

}

/**

* 创建事务

* @param dataSource

* @return

*/

@Bean

@Primary

public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

/**

* 创建模板

* @param sqlSessionFactory

* @return

*/

@Bean

@Primary

public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

SlaveDataSourceConfig.java

package com.core.dataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

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.boot.context.properties.ConfigurationProperties;

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;

/**

* oracle数据配置

*/

@Configuration

//配置mapper文件目录,进行扫描

@MapperScan(basePackages = "com.wawj.core.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate")

public class SlaveDataSourceConfig {

/**

* 创建数据源

* @return

*/

@Bean

@ConfigurationProperties(prefix = "spring.datasource.slave")

public DataSource slaveDataSource() {

return DruidDataSourceBuilder.create().build();

}

/**

* 创建工厂

* @param dataSource

* @return

* @throws Exception

*/

@Bean

public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

//指定mapper.xml的目录

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/**/*.xml"));

//指定实体类目录

bean.setTypeAliasesPackage("com.wawj.core.entity");

return bean.getObject();

}

/**

* 创建事务

* @param dataSource

* @return

*/

@Bean

public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

/**

* 创建模板

* @param sqlSessionFactory

* @return

*/

@Bean

public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

这种方式初始化的是时候在druid控制台(http://localhost:8080/druid/login.html)中的数据源标签是没有,要请求过后才会显示当前连接的数据源

二、使用dynamic-datasource 动态配置(相当于与spring一起创建)

pom.xml

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.10</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>dynamic-datasource-spring-boot-starter</artifactId>

<version>2.5.6</version>

</dependency>

<!-- maven仓库中没有oracle数据库驱动,这里将jar 放到工程下进行引入 -->

<dependency>

<groupId>com.ojdbc</groupId>

<artifactId>ojdbc</artifactId>

<version>10.2.0.1</version>

<scope>system</scope>

<systemPath>${project.basedir}/src/main/lib/ojdbc14-10.2.0.1.0.jar</systemPath>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.38</version>

</dependency>

application.properties

#全局默认值,可以全局更改

spring.datasource.dynamic.druid.initial-size=5

spring.datasource.dynamic.druid.max-active=5

spring.datasource.dynamic.druid.min-idle=5

spring.datasource.dynamic.druid.pool-prepared-statements=true

#oracle数据库

spring.datasource.dynamic.datasource.master.driver-class-name=oracle.jdbc.OracleDriver

spring.datasource.dynamic.datasource.master.druid.validation-query=SELECT 1 FROM DUAL

spring.datasource.dynamic.datasource.master.url=jdbc:oracle:thin:@localhost:1521:test

spring.datasource.dynamic.datasource.master.username=oracle

spring.datasource.dynamic.datasource.master.password=123456

#mysql数据库

spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.dynamic.datasource.slave.druid.validation-query=SELECT 1

spring.datasource.dynamic.datasource.slave.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8

spring.datasource.dynamic.datasource.slave.username=mysql

spring.datasource.dynamic.datasource.slave.password=123456

#默认aihome数据库

spring.datasource.dynamic.primary=master

#排除 原生Druid的快速配置类(DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password等。而我们动态数据源的配置路径是变化的。)

spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

#设置dynamic登录名

spring.datasource.druid.stat-view-servlet.login-username=admin

#设置dynamic密码

spring.datasource.druid.stat-view-servlet.login-password=admin

#mybatis mapper location

mybatis-plus.mapper-locations=classpath:mapper/**/*.xml

mybatis-plus.type-aliases-package=com.wawj.core.entity

这种方式使用注解(@DS("数据源名称"))切换数据源

@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。

注解在service实现或mapper接口方法上,但强烈不建议同时在service和mapper注解。 (可能会有问题)

注解

结果

没有@DS

默认数据源

@DS("dsName")

dsName可以为组名也可以为具体某个库的名称

补充:排除原生的DruidDataSourceAutoConfigure也可以在springboot启动类排除(某些springBoot的版本上面可能无法排除就在application.properties中配置排除)

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

#application.properties

spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

 

以上是 springboot2.1.5+mybatis配置多数据源 的全部内容, 来源链接: utcz.com/z/510410.html

回到顶部