Rumah > Java > javaTutorial > teks badan

springboot采用多数据源对JdbcTemplate进行配置的方法

不言
Lepaskan: 2018-10-11 15:26:27
ke hadapan
2029 orang telah melayarinya

本篇文章给大家带来的内容是关于springboot采用多数据源对JdbcTemplate进行配置的方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

springboot多数据源配置,代码如下

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);
    }

}
Salin selepas log masuk

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);
    }
}
Salin selepas log masuk

配置文件 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
Salin selepas log masuk

测试代码如下

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));

    }
}
Salin selepas log masuk

在运行的时候会出现如下异常问题,运行失败,报出java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.异常

后来经过查资料,发现出现该问题的原因是由于springboot版本的问题,本实例采用的springboot版本为2.0.5,如果将版本改为1.5以前的版本就不会出现如上的问题,其实解决上面的异常主要有如下两种解决方案

方案一:

按照如下方案进行修改application.properties配置文件,如下:修改完成后,上面的异常不会再出现

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
Salin selepas log masuk

方案二:

原有的application.properties配置文件不进行修改,修改DataSourceConfig类中的信息,如下:修改完成后,异常也不会再出现能够正常运行

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
Salin selepas log masuk

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();
    }
}
Salin selepas log masuk

至此,springboot 采用多数据源对JdbcTemplate进行配置完美解决

Atas ialah kandungan terperinci springboot采用多数据源对JdbcTemplate进行配置的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:cnblogs.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!