springboot多数据源配置
程序员文章站
2022-05-24 16:11:44
...
springboot多数据源配置
application.yml
################# dataSource ####################
spring:
datasource:
db1:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
db2:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
其中url必须使用jdbc-url(.yml)或者jdbcUrl(.properties)
启动类Application
package com;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
//加入定时器
@EnableScheduling
@SpringBootApplication
public class AuthApplication {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class, args);
}
}
两个数据源配置类,如果有多个数据源,请自行添加
package com.datasource.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.datasource.dao.test1",sqlSessionFactoryRef = "test1SqlSessionFactory")
public class Test1DataSourceConfig {
@Primary
@Bean(name = "test1DataSource")
@ConfigurationProperties("spring.datasource.db1")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:datasource/dao/test1/*.xml"));
return sessionFactoryBean.getObject();
}
}
package com.datasource.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.datasource.dao.test2",sqlSessionFactoryRef = "test2SqlSessionFactory")
public class Test2DataSourceConfig {
@Primary
@Bean(name = "test2DataSource")
@ConfigurationProperties("spring.datasource.db2")
public DataSource masterDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:datasource/dao/test2/*.xml"));
return sessionFactoryBean.getObject();
}
}
实际上两个配置类是一样的,只需要修改名字就行了
其他的dao接口和映射文件这里就不列出了
推荐阅读
-
SpringBoot 自定义+动态切换数据源教程
-
SpringBoot精简一(配置)
-
springboot项目scheduled定时任务#配置文件取值#jar包动态修改
-
springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
-
Spring配置数据源的三种方式(小结)
-
spring 下配置 dbcp,c3p0,proxool 等数据源连接池
-
spring 下配置 dbcp,c3p0,proxool 等数据源连接池
-
详解多页应用 Webpack4 配置优化与踩坑记录
-
基于webpack实现多html页面开发框架五 开发环境配置 babel配置
-
SpringBoot 配置提示功能