springboot项目毗连多种数据库怎样使用?

瑞星  金牌会员 | 2024-7-23 13:21:18 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 533|帖子 533|积分 1599

在项目标开发中,经常会遇到需要毗连多个多种数据库的情况,mysql、oracle等等,下面详细解说如安在一个服务中进行多种数据库的配置。

第一步:

在yml配置文件中配置多个数据源,如下,根据实际情况更改本身的配置即可。
  1. spring:
  2.   datasource:
  3.     # 配置多个数据源
  4.     primary:
  5.       type: com.alibaba.druid.pool.DruidDataSource
  6.       jdbc-url: jdbc:oracle:thin:@171.28.7.55:1521:example
  7.       username: root
  8.       password: root
  9.       driver-class-name: oracle.jdbc.OracleDriver  #数据库链接驱动
  10.     secondary:
  11.       type: com.alibaba.druid.pool.DruidDataSource
  12.       jdbc-url: jdbc:mysql://127.0.0.1:3306/exinfo?useUnicode=true&characterEncoding=utf-8
  13.       username: root
  14.       password: root
  15.       driver-class-name: com.mysql.cj.jdbc.Driver  #数据库链接驱动
复制代码
第二步:

创建多个配置类,以配置oracle和mysql两个数据库为例,可参考代码进行延展。

1.在配置类中需要进行数据源配置
  1.     @Bean
  2.     @ConfigurationProperties(prefix = "spring.datasource.primary")
  3.     @Primary
  4.     public DataSource db1DataSource() {
  5.         return DataSourceBuilder.create().build();
  6.     }
复制代码
  1. @ConfigurationProperties(prefix = "spring.datasource.primary")用于绑定yml中的第一个数据源配置,这些配置项会被自动映射到db1DataSource所创建的数据源实例中。通过DataSourceBuilder. create()创建一个新的数据源构建器,并调用.build()方法来完成数据源实例的创建。
  2. 如果有多个相同类型的Bean,使用@Primary注解可以标记出一个优先(默认)使用的Bean。所以使用最多的数据库可以使用@Primary注解。
复制代码
2.配置MyBatis的SqlSessionFactory,它是MyBatis使用数据库的核心组件,负责创建SqlSession对象,执行SQL语句等。使用名称为db1DataSource的数据源Bean作为构造SqlSessionFactory的依赖。
  1.     @Bean
  2.     @Primary
  3.     public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
  4.         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  5.         bean.setDataSource(dataSource);
  6.         //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//设置下划线转驼峰式
  7.         return bean.getObject();
  8.     }
复制代码
3.配置事件管理器(DataSourceTransactionManager),它基于数据源(DataSource)的事件管理实现,专门用于JDBC事件处理。注解明确指定使用名为db1DataSource的数据源Bean。
  1.     @Bean
  2.     @Primary
  3.     public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
  4.         return new DataSourceTransactionManager(dataSource);
  5.     }
复制代码
4.配置SqlSessionTemplate实例,它是MyBatis与Spring集成的关键组件,提供了线程安全的SQL会话执行情况。
  1.     @Bean
  2.     @Primary
  3.     public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  4.         return new SqlSessionTemplate(sqlSessionFactory);
  5.     }
复制代码
5.类注解@MapperScan


    1. basePackages= "com.example.mapper":指定了Mapper接口所在的包路径。Spring会扫描这个包及其子包下的所有接口,如果接口符合MyBatis Mapper的规范,Spring会自动生成代理对象来处理SQL调用。
    复制代码
    1. sqlSessionTemplateRef = "db1SqlSessionTemplate":指定了与Mapper接口绑定的sqlSessionTemplate的名称。在执行Mapper接口的方法时,Spring会使用这个指定的sqlSessionTemplate来管理SQL会话。这里的db1SqlSessionTemplate是之前通过@Bean方法定义的sqlSessionTemplate Beam的名称。
    复制代码

DataSourcePrimaryConfig完整代码如下:
  1. package com.example.config;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.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration@MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "db1SqlSessionTemplate")public class DataSourcePrimaryConfig {    @Bean
  2.     @ConfigurationProperties(prefix = "spring.datasource.primary")
  3.     @Primary
  4.     public DataSource db1DataSource() {
  5.         return DataSourceBuilder.create().build();
  6.     }    @Bean
  7.     @Primary
  8.     public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
  9.         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  10.         bean.setDataSource(dataSource);
  11.         //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//设置下划线转驼峰式
  12.         return bean.getObject();
  13.     }    @Bean
  14.     @Primary
  15.     public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
  16.         return new DataSourceTransactionManager(dataSource);
  17.     }    @Bean
  18.     @Primary
  19.     public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  20.         return new SqlSessionTemplate(sqlSessionFactory);
  21.     }}
复制代码

DataSourceSecondaryConfig代码如下:
  1. package com.example.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.boot.jdbc.DataSourceBuilder;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  12. import javax.sql.DataSource;
  13. @Configuration
  14. @MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
  15. public class DataSourceSecondaryConfig {
  16.     @Bean
  17.     @ConfigurationProperties(prefix = "spring.datasource.secondary")
  18.     public DataSource db2DataSource() {
  19.         return DataSourceBuilder.create().build();
  20.     }
  21.     @Bean
  22.     public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
  23.         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  24.         bean.setDataSource(dataSource);
  25.         bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//设置下划线转驼峰式
  26.         return bean.getObject();
  27.     }
  28.     @Bean
  29.     public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
  30.         return new DataSourceTransactionManager(dataSource);
  31.     }
  32.     @Bean
  33.     public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
  34.         return new SqlSessionTemplate(sqlSessionFactory);
  35.     }
  36. }
复制代码

第三步:

根据配置文件,创建两个mapper包如下:

在不同的mapper包下进行不同数据库的交互即可。




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

瑞星

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表