二十七、Spring Boot中多数据源配置之JDBC

版权声明:本文为博主原创文章,未经博主允许欢迎转载,请注明原文链接。一起交流,共同进步。 https://blog.csdn.net/newbie_907486852/article/details/81409975

              Spring Boot中多数据源配置之JDBC

配置文件:

#配置两个数据源,用于测试多数据源连接
#spring.datasource.url 数据库的 JDBC URL。
#spring.datasource.jdbc-url 用来创建连接的 JDBC URL(自定义数据源)

spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver

配置类:

package com.yang.dataSourceConfig;

import javax.sql.DataSource;

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;

@Configuration
public class DataSourceConfig {

   @Bean(name="primaryDataSource")//将这个方法的返回值作为一个Bean放到容器中,并命名为:primaryDataSource
   @Qualifier("primaryDataSource")//注解指定注入的Bean的名称
   @ConfigurationProperties(prefix="spring.datasource.primary")//读取配置文件创建数据源(前缀)
   public DataSource primaryDataSource(){
       return DataSourceBuilder.create().build();
   }


   @Bean(name="seoncodaryDataSource")
   @Qualifier("secondaryDataSource")
   @Primary
   @ConfigurationProperties(prefix="spring.datasource.secondary")
   public DataSource secoundaryDataSource(){
       return DataSourceBuilder.create().build();
   }


   //支持JdbcTemplate实现多数据源             @Qualifier("primaryDataSource"):使用ByName的方式注入数据源
   @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);
       }
   }

测试类:

package com.yang.repo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import com.yang.domain.User;
import com.yang.repo.UserServiceRepo;

@Service
public class UserServiceImpl implements UserServiceRepo{


    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate primaryJdbcTemplate;


    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    private JdbcTemplate secondaryJdbcTemplate;



    @Override
    public User selectUserByID(Integer id) {
        String sql="select * from user_jdbc where id=?";
        RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        User user=primaryJdbcTemplate.queryForObject(sql, rowMapper, id);

        return user;
    }


}
展开阅读全文

没有更多推荐了,返回首页