springboot系列10: 多数据源
作者:互联网
多数据源配置实战(整合MyBatis)
-
首先要在@SpringBootApplication排除该类,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源
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
# 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
-
手动创建数据库配置类
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:
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:
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库。
标签:10,springboot,spring,org,springframework,datasource,数据源,db1,import 来源: https://www.cnblogs.com/yb-ken/p/15799368.html