springboot添加多数据源的方法实例教程

 更新时间:2023年09月12日 10:51:51   作者:清风怎不知意  
这篇文章主要给大家介绍了关于springboot添加多数据源方法的相关资料,在实际开发中经常可能遇到在一个应用中可能要访问多个数据库多的情况,需要的朋友可以参考下

前言

Spring Boot可以通过配置多个数据源来支持多数据源访问,以下是一个基本的多数据源配置实例:

添加多数据源的配置类

创建一个配置类来配置多个数据源,可以使用@Configuration和@Primary注解来标识主数据源,示例代码如下:

@Configuration
public class DataSourceConfig {
    @Bean(name = "primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

在这个配置类中,我们定义了两个数据源,一个是主数据源,一个是次要数据源。我们使用@ConfigurationProperties注解来设置每个数据源的配置参数。

在application.yml中添加数据源配置

添加application.yml配置文件,并在文件中添加多个数据源的配置信息,例如:

spring:
  datasource:
    primary:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test1
      username: root
      password: root
    secondary:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test2
      username: root
      password: root

我们使用了Spring Boot的默认配置规则,通过指定数据源的名称和属性前缀来配置每个数据源。

配置JdbcTemplate来访问多个数据源

现在我们已经定义了两个数据源,我们需要配置JdbcTemplate来访问它们。我们可以在使用JdbcTemplate的每个方法中指定要使用的数据源,示例代码如下:

@Repository
public class UserRepository {
    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;
    @Autowired
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;
    public List<User> getUsersFromPrimary() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(primaryDataSource);
        List<User> users = jdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper(User.class));
        return users;
    }
    public List<User> getUsersFromSecondary() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(secondaryDataSource);
        List<User> users = jdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper(User.class));
        return users;
    }
}

我们在注入JdbcTemplate之前使用@Autowired注解将每个数据源注入到存储库类中。然后,我们可以在每个方法中使用JdbcTemplate来访问不同的数据源。

这是一个基本的多数据源配置实例,你可以在此基础上进一步调整以满足你的具体需求。

如果你使用的是MyBatis框架,你可以使用MyBatis的动态数据源来实现多数据源配置,具体步骤如下

定义数据源配置类

首先,你需要定义一个数据源配置类来读取和解析配置文件,然后根据配置创建数据源并将其添加到数据源列表中,示例代码如下:

@Configuration
public class DataSourceConfig {
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().type(dataSourceType).build();
    }
    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().type(dataSourceType).build();
    }
    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource());
        targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource());
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
        return dynamicDataSource;
    }
}

在这个配置类中,我们使用@ConfigurationProperties注解来读取每个数据源的配置信息,然后使用DataSourceBuilder创建数据源对象。接着,我们使用DynamicDataSource类来管理多个数据源,将所有数据源添加到targetDataSources中并设置默认数据源为masterDataSource。

实现动态数据源

接下来,你需要实现一个动态数据源,它可以根据需要选择不同的数据源,示例代码如下:

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceContextHolder.getDataSourceType();
    }
}

我们继承了AbstractRoutingDataSource类,覆盖了它的determineCurrentLookupKey()方法来获取当前线程所需的数据源类型,并返回数据源名称。

实现数据源上下文

最后,你需要创建一个数据源上下文来保存当前线程所需的数据源类型,示例代码如下:

public class DynamicDataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }
    public static String getDataSourceType() {
        return contextHolder.get();
    }
    public static void clearDataSourceType() {
        contextHolder.remove();
    }
}

我们使用了ThreadLocal来保存当前线程所需的数据源类型。你可以在需要访问不同数据源的方法中调用setDataSourceType()方法来设置当前线程所需的数据源类型,调用clearDataSourceType()方法来清除上下文。

最后,在你的MyBatis配置文件中,你可以使用${}占位符引用定义的动态数据源,示例代码如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dynamicDataSource"/>
    <property name="typeAliasesPackage" value="com.example.model"/>
</bean>

我们将MyBatis的SqlSessionFactoryBean中的dataSource属性设置为定义的动态数据源dynamicDataSource即可。

切换数据源

最后,你可以在需要访问不同数据源的方法中调用DynamicDataSourceContextHolder.setDataSourceType()方法来设置当前线程所需的数据源类型。例如,你可以定义一个注解来标识使用哪个数据源,然后在方法上加上这个注解即可自动切换数据源。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
    DataSourceType value() default DataSourceType.MASTER;
}
@Aspect
@Component
public class DynamicDataSourceAspect {
    @Pointcut("@annotation(com.example.annotation.DataSource)")
    public void dataSourcePointcut() {
    }
    @Before("dataSourcePointcut()")
    public void before(JoinPoint joinPoint) {
        DataSource dataSource = getDataSource(joinPoint);
        if (dataSource != null) {
            DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
        }
    }
    @After("dataSourcePointcut()")
    public void after(JoinPoint joinPoint) {
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
    private DataSource getDataSource(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(DataSource.class)) {
            return method.getAnnotation(DataSource.class);
        }
        return null;
    }
}

在这个例子中,我们定义了一个@DataSource注解来标识使用哪个数据源,然后使用AOP来自动切换数据源。在DynamicDataSourceAspect类中,我们使用@Pointcut注解来定义切点,然后使用@Before和@After注解来分别在方法调用前和方法调用后切换数据源。在切换数据源时,我们可以通过getDataSource()方法获取方法上的@DataSource注解,并使用注解中定义的数据源类型来设置当前线程所需的数据源类型。

总结

到此这篇关于springboot添加多数据源方法的文章就介绍到这了,更多相关springboot添加多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring依赖注入中的@Resource与@Autowired详解

    Spring依赖注入中的@Resource与@Autowired详解

    这篇文章主要介绍了Spring依赖注入中的@Resource与@Autowired详解,提到Spring依赖注入,大家最先想到应该是@Resource和@Autowired,对于Spring为什么要支持两个这么类似的注解却未提到,属于知其然而不知其所以然,本文就来做详细讲解,需要的朋友可以参考下
    2023-09-09
  • 基于JAVA每月运势api调用代码实例

    基于JAVA每月运势api调用代码实例

    这篇文章主要为大家详细介绍了JAVA每月运势api调用代码实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • MyBatis中多对多关系的映射和查询

    MyBatis中多对多关系的映射和查询

    本文主要介绍了MyBatis中多对多关系的映射和查询的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 如何在logback日志配置里获取服务器ip和端口

    如何在logback日志配置里获取服务器ip和端口

    这篇文章主要介绍了如何在logback日志配置里获取服务器ip和端口的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Spring中的事件监听器使用学习记录

    Spring中的事件监听器使用学习记录

    Spring框架中的事件监听机制是一种设计模式,它允许你定义和触发事件,同时允许其他组件监听这些事件并在事件发生时作出响应,这篇文章主要介绍了Spring中的事件监听器使用学习,需要的朋友可以参考下
    2024-07-07
  • java实现俄罗斯方块

    java实现俄罗斯方块

    这篇文章主要为大家详细介绍了java实现俄罗斯方块,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Javaweb项目session超时解决方案

    Javaweb项目session超时解决方案

    这篇文章主要介绍了Javaweb项目session超时解决方案,关于解决方案分类比较明确,内容详细,需要的朋友可以参考下。
    2017-09-09
  • SpringBoot项目中Druid自动登录功能实现

    SpringBoot项目中Druid自动登录功能实现

    Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能,这篇文章主要介绍了SpringBoot项目中Druid自动登录功能实现,需要的朋友可以参考下
    2024-08-08
  • Spring AOP实现Redis缓存数据库查询源码

    Spring AOP实现Redis缓存数据库查询源码

    这篇文章主要介绍了Spring AOP实现Redis缓存数据库查询的相关内容,源码部分还是不错的,需要的朋友可以参考下。
    2017-09-09
  • Eclipse添加xml文件提示及Hibernate配置学习

    Eclipse添加xml文件提示及Hibernate配置学习

    文件提示功能在开发过程中很实用的,本文实现了一个Eclipse添加xml文件提示,感兴趣的朋友可以了解下啊,希望本文对你有所帮助
    2013-01-01

最新评论