Home  >  Article  >  Java  >  Springboot uses multiple data sources to configure JdbcTemplate

Springboot uses multiple data sources to configure JdbcTemplate

不言
不言forward
2018-10-11 15:26:272038browse

The content of this article is about springboot's method of configuring JdbcTemplate using multiple data sources. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

springboot multiple data source configuration, the code is as follows

DataSourceConfig

package com.rookie.bigdata.config;

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;

/**
 * @author
 * @date 2018/10/10
 */
@Configuration
public class DataSourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    @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);
    }

}

StudentServiceImpl

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author
 * @date 2018/10/9
 */
@Service
public class StudentServiceImpl implements StudentService {

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

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

    /**
     * 采用第一个暑假源进行插入数据
     * @param student
     */
    @Override
    public void create(Student student) {

        jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge());

    }

    /**
     * 第一个数据源进行插入数据
     * @param stuNo
     */
    @Override
    public void deleteByNo(Integer stuNo) {
        jdbcTemplate.update("DELETE  FROM  student WHERE stu_no=?", stuNo);
    }

    /**
     * 第二个数据源进行查询数据
     * @param stuNo
     * @return
     */
    @Override
    public Integer queryByStuNo(Integer stuNo) {
        return jdbcTemplate2.queryForObject("select count(1) from student", Integer.class);
    }
}

Configuration file application.properties

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

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

The test code is as follows

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author liuxili
 * @date 2018/10/10
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceImplTest {

    @Autowired
    private StudentServiceImpl studentService;

    @Test
    public void create1() throws Exception {
        Student student = new Student();
        student.setStuNo(1L);
        student.setName("张三");
        student.setAge(23);
        studentService.create(student);
    }
    @Test
    public void deleteByName1() throws Exception {
        studentService.deleteByNo(1);
    }
    @Test
    public void queryByStuNo1() throws Exception {
        System.out.println(studentService.queryByStuNo(1));

    }
}

The following exception will occur during operation. The operation fails and java.lang.IllegalArgumentException is reported: jdbcUrl is required with driverClassName.Exception

After checking the information, I found that the reason for this problem is due to the springboot version. The springboot version used in this example is 2.0.5. If the version is changed to a version before 1.5, it will not work. The above problems will occur. In fact, there are two main solutions to solve the above exceptions.

Solution 1:

Modify the application.properties configuration file according to the following scheme, as follows: After the modification is completed, The above exception will not occur again

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

Option 2:

Do not modify the original application.properties configuration file, modify the information in the DataSourceConfig class, as follows: After the modification is completed, the exception will It will no longer appear that it can run normally

application.properties

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

DataSourceConfig

package com.rookie.bigdata.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
 * @author
 * @date 2018/10/10
 */
@Configuration
public class DataSourceConfig {
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Primary
    @Bean(name = "primaryDataSourceProperties")
    @Qualifier("primaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "secondaryDataSourceProperties")
    @Qualifier("secondaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSourceProperties secondaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {

        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {

        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }
}

At this point, springboot uses multiple data sources to configure JdbcTemplate and has a perfect solution

The above is the detailed content of Springboot uses multiple data sources to configure JdbcTemplate. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete