• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

本篇文章给大家带来的内容是关于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 */@Configurationpublic 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 */@Servicepublic class StudentServiceImpl implements StudentService {    @Autowired    @Qualifier("primaryJdbcTemplate")    private JdbcTemplate jdbcTemplate;    @Autowired    @Qualifier("secondaryJdbcTemplate")    private JdbcTemplate jdbcTemplate2;    /**     * 采用第一个暑假源进行插入数据     * @param student     */    @Override    public void create(S<em>本文来源[email protected]搞@^&代*@码)网5</em>tudent 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);    }}

配置文件 application.properties

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

测试代码如下

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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot采用多数据源对JdbcTemplate进行配置的方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址