写点什么

Spring Boot MyBatis 配置 Druid 多数据源

  • 2022 年 5 月 14 日
  • 本文字数:2522 字

    阅读完需:约 8 分钟

</dependency>


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


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


<version>1.3.1</version>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


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


<scope>test</scope>


</dependency>


<dependency>


<groupId>com.oracle</groupId>


<artifactId>ojdbc6</artifactId>


<version>11.2.0.4</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


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


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


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


<version>1.1.6</version>


</dependency>


然后根据 application.yml 创建两个数据源配置类 MysqlDatasourceConfig 和 OracleDatasourceConfig:


MysqlDatasourceConfig:


package com.springboot.datasource;


import javax.sql.DataSource;


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.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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;


@Configuration


@MapperScan(basePackages = MysqlDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")


public class MysqlDatasourceConfig {


// mysqldao 扫描路径


static final String PACKAGE = "com.springboot.mysqldao";


// mybatis mapper 扫描路径


static final String MAPPER_LOCATION = "classpath:mapper/mysql/*.xml";


@Primary


@Bean(name = "mysqldatasource")


@ConfigurationProperties("spring.datasource. 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 druid.mysql")


public DataSource mysqlDataSource() {


return DruidDataSourceBuilder.create().build();


}


@Bean(name = "mysqlTransactionManager")


@Primary


public DataSourceTransactionManager mysqlTransactionManager() {


return new DataSourceTransactionManager(mysqlDataSource());


}


@Bean(name = "mysqlSqlSessionFactory")


@Primary


public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqldatasource") DataSource dataSource)


throws Exception {


final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();


sessionFactory.setDataSource(dataSource);


//如果不使用 xml 的方式配置 mapper,则可以省去下面这行 mapper location 的配置。


sessionFactory.setMapperLocations(


new PathMatchingResourcePatternResolver().getResources(MysqlDatasourceConfig.MAPPER_LOCATION));


return sessionFactory.getObject();


}


}


上面代码配置了一个名为 mysqldatasource 的数据源,对应 application.yml 中spring.datasource.druid.mysql前缀配置的数据库。然后创建了一个名为 mysqlSqlSessionFactory 的 Bean,并且注入了 mysqldatasource。与此同时,还分别定了两个扫描路径 PACKAGE 和 MAPPER_LOCATION,前者为 Mysql 数据库对应的 mapper 接口地址,后者为对应的 mapper xml 文件路径。


@Primary标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。多数据源配置的时候,必须要有一个主数据源,用@Primary标志该 Bean。


同理,接着配置 Oracle 数据库对应的配置类:


OracleDatasourceConfig:


@Configuration


@MapperScan(basePackages = OracleDatasourceConfig.PACKAGE,


sqlSessionFactoryRef = "oracleSqlSessionFactory")


public class OracleDatasourceConfig {


// oracledao 扫描路径


static final String PACKAGE = "com.springboot.oracledao";


// mybatis mapper 扫描路径


static final String MAPPER_LOCATION = "classpath:mapper/oracle/*.xml";


@Bean(name = "oracledatasource")


@ConfigurationProperties("spring.datasource.druid.oracle")


public DataSource oracleDataSource() {


return DruidDataSourceBuilder.create().build();


}


@Bean(name = "oracleTransactionManager")


public DataSourceTransactionManager oracleTransactionManager() {


return new DataSourceTransactionManager(oracleDataSource());


}


@Bean(name = "oracleSqlSessionFactory")


public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracledatasource") DataSource dataSource)


throws Exception {


final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();


sessionFactory.setDataSource(dataSource);


//如果不使用 xml 的方式配置 mapper,则可以省去下面这行 mapper location 的配置。


sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()


.getResources(OracleDatasourceConfig.MAPPER_LOCATION));


return sessionFactory.getObject();


}


}


配置完多数据源,接下来分别在 com.springboot.mysqldao 路径和 com.springboot.oracledao 路径下创建两个 mapper 接口:


MysqlStudentMapper:


package com.springboot.mysqldao;


import java.util.List;


import java.util.Map;


import org.apache.ibatis.annotations.Mapper;


@Mapper


public interface MysqlStudentMapper {


List<Map<String, Object>> getAllStudents();


}


OracleStudentMapper:


package com.springboot.oracledao;


import java.util.List;


import java.util.Map;


import org.apache.ibatis.annotations.Mapper;


@Mapper


public interface OracleStudentMapper {


List<Map<String, Object>> getAllStudents();


}

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Spring Boot MyBatis配置Druid多数据源_Java_爱好编程进阶_InfoQ写作社区