参考1: https://www.cnblogs.com/tangzekai/p/7782773.html

 

参考2  https://blog.csdn.net/baochanghong/article/details/54134018

 

依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>

 

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
  </dependency>

 

jdbcTemplate:

 

List<ReportDetailInfo> list = ediPdJdbcTemplate.query(getShipSql,new BeanPropertyRowMapper(ReportDetailInfo.class)) ;

 

 

配置:

package com.icil.report.config;

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")
    @Qualifier("primaryDataSource")//原博主这里加上@Qualify 没有必要 删了也没有影响
    @ConfigurationProperties("jdbc.datasource.edi")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
 
//    @Primary
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource") //原博主这里加上@Qualify 没有必要 删了也没有影响
    @ConfigurationProperties("jdbc.datasource.booking")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
 
 
    /**
     * @param dataSource
     * @return
     * @Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法, 并且交给Spring容器管理;从这我们就明白了为啥@Bean是放在方法的注释上了,
     * 因为它很明确地告诉被注释的方法,你给我产生一个Bean,然后交给Spring容器,
     * 实现依赖注入
     */
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
 
    
    
    
    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
 
 
}
View Code

 

application.properties

### database EDI configure
jdbc.datasource.edi.jdbc-url=jdbc:mysql:///EDI
jdbc.datasource.edi.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.datasource.edi.username=root
jdbc.datasource.edi.password=hhaa
jdbc.datasource.edi.sql-script-encoding=UTF-8


### database booking configure
jdbc.datasource.booking.jdbc-url=jdbc:mysql:///booking
jdbc.datasource.booking.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.datasource.booking.username=root
jdbc.datasource.booking.password=hahahaha
jdbc.datasource.booking.sql-script-encoding=UTF-8
View Code

 

 

test:

package top.zekk.twodatasource;
 
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class TwoDatasourceApplicationTests {
 
    @Autowired
    /*@Qualify 多个同类型bean?存在时  Spring不知道应该绑定哪个实现类
    指定绑定的类名@Bean(name="**")
    */
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate1;
 
    @Autowired
    //@Qualify 多个同类型bean?存在时 指定绑定的类名@Bean(name="**")
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate2;
 
    @Before //首先执行清空数据库操作
    public void setUp(){
        jdbcTemplate1.update("DELETE FROM USER ");
        jdbcTemplate2.update("DELETE FROM USER ");
    }
 
    @Test
    public void test(){
        jdbcTemplate1.update
                ("INSERT INTO USER (id,name,age) VALUES (?,?,?)",1,"aaa",20);
        jdbcTemplate2.update
                ("INSERT INTO USER (id,name,age) VALUES (?,?,?)",1,"bbb",30);
        jdbcTemplate2.update
                ("INSERT INTO USER (id,name,age) VALUES (?,?,?)",2,"bbb",31);
 
        Assert.assertEquals("1",jdbcTemplate1.queryForObject("select count(1) from user",String.class));
 
        Assert.assertEquals("2",jdbcTemplate2.queryForObject("select count(1) from user",String.class));
    }
 
}
View Code

 

    @Test  // 136 db
    void TestInsert() {
       userJdbcTemplate.update
               ("INSERT INTO user (id,name,age) VALUES (?,?,?)",4,"Ryan1",21);
       addressJdbcTemplate.update
               ("INSERT INTO address (id,country,provinces,city) VALUES (?,?,?,?)",4,"cn1","gd1","sz");

       List<Map<String, Object>> mapsUser = userJdbcTemplate.queryForList("select * from user");
       List<Map<String, Object>> mapsAddress = addressJdbcTemplate.queryForList("select * from address");
       System.err.println("user" + mapsAddress);
       System.err.println("address" + mapsAddress);
    }

 

posted on 2019-04-29 16:36  lshan  阅读(151)  评论(0编辑  收藏  举报