springboot jdbctemplate如何实现多数据源

 更新时间:2024年07月11日 15:15:15   作者:在下,杨江河  
这篇文章主要介绍了springboot jdbctemplate如何实现多数据源问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

1.简介

所谓多数据源,其实就是在一个项目中使用多个数据库实例中的数据库或者同一个数据库实例中多个不同的库。

在大部分情况下会使用更加强大的持久化框架来访问数据库,比如MyBatis、Hibernate或者Spring Data JPA等ORM框架。

使用JDBC是开发者必备的基础技能,只有熟悉了基础的JDBC,才能更加深入地学习其他的ORM框架。

2.举例

2.1配置多数据源连接信息

spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/jdbctest
spring.datasource.primary.username=root
spring.datasource.primary.password=Yjb123456
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/jdbctest2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=Yjb123456
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver

2.2配置JDBC初始化

创建DataSourceConfig类,在项目启动时读取配置文件中的数据库信息,并对JDBC初始化,具体代码如下:

在上面的示例中,DataSourceConfig类的作用是在项目启动时根据特定的前缀加载不同的数据源,再根据构建好的数据源创建不同的JdbcTemplate。

由于Spring容器中存在两个数据源,使用默认的类型查找时会报错,因此加上@Qualifier注解,表示按照名称查找。

这里创建了两个JdbcTemplate实例,分别对应了两个数据源。

需要注意的是,使用多个数据源时需要添加@Primary注解,表示自动装配出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者。

Primary表示“主要的”,类似于SQL语句中的“Primary Key”(主键),只能有唯一一个,否则会报错。

package com.yangjunbo.helloword.properties;

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.core.JdbcTemplate;

import javax.sql.DataSource;

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

2.3测试调用多数据源

package com.yangjunbo.helloword;

import com.yangjunbo.helloword.pojo.Student;
import com.yangjunbo.helloword.rowMapper.StudentRowMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class RowMapper {
   
    @Autowired
    private JdbcTemplate primaryJdbcTemplate;

    @Autowired
    private JdbcTemplate secondaryJdbcTemplate;

    @Test
    public void dataSourceTest(){
        Student student = new Student("weiz多数据源",0,30);
        primaryJdbcTemplate.update("INSERT INTO Student(name, sex, age) values(?, ?, ?)",
                student.getName(), student.getSex(), student.getAge());

        secondaryJdbcTemplate.update("INSERT INTO Student(name, sex, age) values(?, ?, ?)",
                student.getName(), student.getSex(), student.getAge());
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot 项目启动后无日志输出直接结束的解决

    springboot 项目启动后无日志输出直接结束的解决

    这篇文章主要介绍了springboot 项目启动后无日志输出直接结束的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • SpringCloud Feign 传输Date类型参数存在误差的问题

    SpringCloud Feign 传输Date类型参数存在误差的问题

    这篇文章主要介绍了SpringCloud Feign 传输Date类型参数存在误差的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • springboot 使用上下文获取bean

    springboot 使用上下文获取bean

    这篇文章主要介绍了springboot 使用上下文获取bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 如何在Spring Boot中使用MQTT

    如何在Spring Boot中使用MQTT

    这篇文章主要介绍了如何在Spring Boot中使用MQTT,帮助大家更好的理解和学习使用Spring Boot,感兴趣的朋友可以了解下
    2021-04-04
  • Java中的ZooKeeper使用

    Java中的ZooKeeper使用

    本文主要介绍了Java中的ZooKeeper使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • J2EE基础之EJB全面了解

    J2EE基础之EJB全面了解

    下面小编就为大家带来一篇J2EE基础之EJB全面了解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • Java读写文件创建文件夹多种方法示例详解

    Java读写文件创建文件夹多种方法示例详解

    这篇文章主要介绍了Java读写文件创建文件夹等多种操作的方法,大家参考使用吧
    2013-11-11
  • 一文解开java中字符串编码的小秘密(干货)

    一文解开java中字符串编码的小秘密(干货)

    这篇文章主要介绍了一文解开java中字符串编码的小秘密(干货),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 快速解决 IDEA 报错: “java 找不到符号“(“cannot find symbol“)

    快速解决 IDEA 报错: “java 找不到符号“(“cannot find symbol“)

    文章详细讲解了在IntelliJIDEA中解决“找不到符号”错误的方法,包括检查导入语句、拼写错误、类路径设置、文件编译状态、JDK配置以及IDE配置问题,通过具体示例代码,展示了如何从错误代码到解决步骤,感兴趣的朋友一起看看吧
    2025-03-03
  • Spring中@Value注解的使用方法详解

    Spring中@Value注解的使用方法详解

    这篇文章主要介绍了Spring中@Value注解的使用方法详解,在spring项目中必不可少的就是读取配置文件,那么读取配置文件就有两种方式,一种就是使用Spring中@Value注解,还有一种是使用SpringBoot中的@ConfigurationProperties注解,需要的朋友可以参考下
    2024-01-01

最新评论