话不多说,直接上代码例子
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
properties文件:
##################################################################
spring.datasource.data1.jdbc-url=jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.data1.username=root
spring.datasource.data1.password=root
spring.datasource.data1.driverClassName=com.mysql.cj.jdbc.Driver
################################################################
spring.datasource.data2.jdbc-url=jdbc:mysql://localhost:3308/demo2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.data2.username=root
spring.datasource.data2.password=root
spring.datasource.data2.driverClassName=com.mysql.cj.jdbc.Driver
##############################################################
spring.aop.proxy-target-class=true
spring.aop.auto=true
###################################################################
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.auto-mapping-behavior=full
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
###################################################################
# 服务监听端口配置
##################################################################
server.port=8100
server.context-path=/
configuration配置文件:
########
datasource1:
@Configuration//注解到spring容器中
@MapperScan(basePackages = "com.test.mapper.mapper1",sqlSessionFactoryRef = "data1SqlSessionFactory")
@ConditionalOnProperty(value ="spring.datasource.data1.jdbc-url")
public class DataSource1 {
/** 返回data1数据库的数据源
* @return
**/
@Bean(name="data1Source")
@Primary//主数据源
@ConfigurationProperties(prefix = "spring.datasource.data1")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
/*
* 返回data1数据库的会话工厂
* @param ds
* @return
* @throws Exception
**/
@Bean(name = "data1SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("data1Source") DataSource ds) throws Exception{
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(ds);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper1/**.xml"));
return bean.getObject();
}
/* * 返回data1数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
**/
@Bean(name = "data1SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data1SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
/* * 返回data1数据库的事务
* @param ds
* @return
**/
@Bean(name = "data1TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("data1Source") DataSource ds){
return new DataSourceTransactionManager(ds);
}
}
#######
datasource2:
@Configuration//注解到spring容器中
@MapperScan(basePackages = "com.test.mapper.mapper2", sqlSessionFactoryRef = "data2SqlSessionFactory")
@ConditionalOnProperty(value ="spring.datasource.data2.jdbc-url")
public class DataSource2 {
/*
*
* 返回data2数据库的数据源
* @return
*/
@Bean(name = "data2Source")
@ConfigurationProperties(prefix = "spring.datasource.data2")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
/**
* 返回data2数据库的会话工厂
*
* @param ds
* @return
* @throws Exception
*/
@Bean(name = "data2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("data2Source") DataSource ds) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(ds);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/**.xml"));
return bean.getObject();
}
/**
* 返回data2数据库的会话模板
*
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "data2SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception {
return new SqlSessionTemplate(sessionFactory);
}
/**
* 返回data2数据库的事务
*
* @param ds
* @return
*/
@Bean(name = "data2TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("data2Source") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
}
启动类:
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
这是项目结构:
至此多数据源配置就完成了。接下来就可以将不同数据库的数据进行操作了