时间:2021-07-01 10:21:17 帮助过:21人阅读
注:github中源码依赖是精简后的结果.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring's support of jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring's support of test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
public class Application {
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
}
src/main/resources/application.yml
src:
datasource:
jdbc-url: jdbc:mysql://{serverIP}:{port}/{db}?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST
username: user
password: pwd
target:
datasource:
jdbc-url: jdbc:mysql://{serverIP}:{port}/{db}?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST
username: user
password: pwd
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;
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "srcDs")
@ConfigurationProperties(prefix = "src.datasource")
public DataSource srcDs() {
return DataSourceBuilder.create().build();
}
@Bean(name = "targetDs")
@ConfigurationProperties(prefix = "target.datasource")
public DataSource targetDs() {
return DataSourceBuilder.create().build();
}
@Bean(name = "srcJdbcTemplate")
public JdbcTemplate srcJdbcTemplate(
@Qualifier("srcDs") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "targetJdbcTemplate")
public JdbcTemplate targetJdbcTemplate(
@Qualifier("targetDs") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
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.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DataSourcesTest {
@Autowired
@Qualifier("srcJdbcTemplate")
protected JdbcTemplate srcJdbcTemplate;
@Autowired
@Qualifier("targetJdbcTemplate")
protected JdbcTemplate targetJdbcTemplate;
@Test
public void test() throws Exception {
List<Map<String, Object>> srcMaps = srcJdbcTemplate.queryForList(" select * from user limit 10 ");
List<Map<String, Object>> targetMaps = targetJdbcTemplate.queryForList(" select * from user limit 10 ");
return;
}
}
关于springboot访问多个mysql库
标签:config throw code rgs java 取消 ann Fix classes