springboot系列10: 多数据源
多数据源配置实战(整合MyBatis)
-
首先要在@SpringBootApplication排除该类,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example.multi.datasource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication (exclude = { DataSourceAutoConfiguration. class }) public class SpringbootStartApplication { public static void main(String[] args) { SpringApplication.run(SpringbootStartApplication. class ,args); } } |
-
在application.properties中配置多数据源连接信息,需要几个就配置几个,名字可以自由命名代替db1,db2
1 2 3 4 5 6 7 8 9 10 11 12 | # database db.conn.str = useUnicode= true &characterEncoding=UTF- 8 &zeroDateTimeBehavior=convertToNull&useLocalSessionState= true &tinyInt1isBit= false &serverTimezone=GMT%2B8 spring.datasource.db1.jdbc-url=jdbc:mysql: //localhost:3306/multi_test1?${db.conn.str} spring.datasource.db1.username=root spring.datasource.db1.password= 111111 spring.datasource.db1.driver- class -name=com.mysql.jdbc.Driver spring.datasource.db2.jdbc-url=jdbc:mysql: //localhost:3306/multi_test2?${db.conn.str} spring.datasource.db2.username=root spring.datasource.db2.password= 111111 spring.datasource.db2.driver- class -name=com.mysql.jdbc.Driver |
注意:这里请一定将spring.datasource.db1.url改为spring.datasource.db1.jdbc-url
-
手动创建数据库配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.example.multi.datasource.config; 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 javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean (name = "db1" ) @ConfigurationProperties (prefix = "spring.datasource.db1" ) public DataSource businessDbDataSource() { return DataSourceBuilder.create().build(); } @Bean (name = "db2" ) @ConfigurationProperties (prefix = "spring.datasource.db2" ) public DataSource newhomeDbDataSource() { return DataSourceBuilder.create().build(); } } |
-
分别配置不同数据源的mybatis的SqlSessionFactory
创建Db1Config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.example.multi.datasource.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration @MapperScan (basePackages = { "com.example.multi.datasource.mapper.db1" }, sqlSessionFactoryRef = "sqlSessionFactoryDb1" ) public class Db1Config { @Autowired @Qualifier ( "db1" ) private DataSource dataSourceDb1; @Bean public SqlSessionFactory sqlSessionFactoryDb1() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSourceDb1); factoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources( "classpath*:mapper/db1/*.xml" )); return factoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception { return new SqlSessionTemplate(sqlSessionFactoryDb1()); } } |
创建Db2Config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.example.multi.datasource.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration @MapperScan (basePackages = { "com.example.multi.datasource.mapper.db2" }, sqlSessionFactoryRef = "sqlSessionFactoryDb2" ) public class Db2Config { @Autowired @Qualifier ( "db2" ) private DataSource dataSourceDb2; @Bean public SqlSessionFactory sqlSessionFactoryDb2() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSourceDb2); factoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources( "classpath*:mapper/db2/*.xml" )); return factoryBean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception { return new SqlSessionTemplate(sqlSessionFactoryDb2()); } } |
注意:此步一定要添加mapper.xml文件扫描路径,否则报错Invalid bound statement (not found)
-
完成这些配置后,假设我们有2个Mapper mapper.db1.xxxMapper和mapper.db2.xxxMapper,我们在程序的任何位置使用前者时会自动连接db1库,后者连接db2库。
分类:
spring boot
标签:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了