您好,登錄后才能下訂單哦!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demoT002</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demoT002</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- MySql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#數據源1
spring.datasource.titan-master.jdbc-url=jdbc:mysql://192.168.7.284:3306/account?useUnicode=true&characterEncoding=utf-8
spring.datasource.titan-master.username=uat_api_java
spring.datasource.titan-master.password=54GDfethu634tIi6778FjsB
spring.datasource.titan-master.driver-class-name=com.mysql.jdbc.Driver
#數據源2
spring.datasource.db2.jdbc-url=jdbc:mysql://192.168.7.284:3306/basic_business?useUnicode=true&characterEncoding=utf-8
spring.datasource.db2.username=uat_api_java
spring.datasource.db2.password=54GDfethurSDFvhyU5634tIFjsB
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
注意這邊不是url而使用的是jdbc-url
主文件中:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//exclude 排除自動讀取數據源 需要添加兩個數據庫
@ServletComponentScan
public class DemoT002Application {
public static void main(String[] args) {
SpringApplication.run(DemoT002Application.class, args);
}
}
DataSource配置:
@Configuration
public class DataSourceConfig {
@Bean(name = "ds1")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.titan-master") // application.properteis中對應屬性的前綴
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ds2")
@ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中對應屬性的前綴
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
}
第一個連接配置:
@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.account"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
@Autowired
@Qualifier("ds1")
private DataSource ds1;
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1); // 使用titan數據源, 連接titan庫
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/account/*.xml"));
return factoryBean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
return template;
}
}
第二個連接配置:
@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.basicbusiness"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
@Autowired
@Qualifier("ds2")
private DataSource ds2;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds2);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/basicbusiness/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。