SpringBoot怎么配置多数据源
这篇文章主要介绍了SpringBoot怎么配置多数据源,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
一、建库建表1.1 创建数据库db1和数据库db21.2 在数据库db1中创建表db1CREATETABLE`db1`(`id`intunsignedzerofillNOTNULLAUTO_INCREMENT,`name`varchar(50)DEFAULTNULL,`age`intunsignedzerofillDEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=utf8mb4;1.3 在数据库db2中创建表db2
CREATETABLE`db2`(`id`intunsignedzerofillNOTNULLAUTO_INCREMENT,`name`varchar(50)DEFAULTNULL,`age`intunsignedzerofillDEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=utf8mb4;二、创建springboot项目2.1 pom.xml导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></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>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>2.2 创建application.yml文件(与 2.3 二选一进行配置,推荐此方法)
server:port:8080#启动端口spring:datasource:db1:#数据源1jdbc-url:jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8username:rootpassword:rootdriver-class-name:com.mysql.cj.jdbc.Driverdb2:#数据源2jdbc-url:jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8username:rootpassword:rootdriver-class-name:com.mysql.cj.jdbc.Driver2.3 创建application.properties文件(与 2.2 二选一进行配置)
server.port=8080spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8spring.datasource.db1.username=rootspring.datasource.db1.password=rootspring.datasource.db1.driver-class-name=com.mysql.jdbc.Driverspring.datasource.db2.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8spring.datasource.db2.username=rootspring.datasource.db2.password=rootspring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver2.4 创建mapper文件
我个人是放在mapper包下,文件随便命名的
代码随便写的,测试而已
importorg.apache.ibatis.annotations.Insert;importorg.apache.ibatis.annotations.Mapper;/***@Authorif*@Description:Whatisit*@Date2021-05-20下午09:52*/@MapperpublicinterfaceDb1Mapper{@Insert("insertintodb1(name,age)values('if',18)")intadd();}
importorg.apache.ibatis.annotations.Insert;importorg.apache.ibatis.annotations.Mapper;/***@Authorif*@Description:Whatisit*@Date2021-05-20下午09:52*/@MapperpublicinterfaceDb2Mapper{@Insert("insertintodb2(name,age)values('fi',81)")intadd();}2.5 创建config配置文件
我个人是放在config包下,文件随便命名的
importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.mybatis.spring.SqlSessionTemplate;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.boot.jdbc.DataSourceBuilder;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importjavax.sql.DataSource;/***@Authorif*@Description:注意以下有些文件路径需要更改*@Date2021-05-20下午09:56*/@Configuration@MapperScan(basePackages="com.ifyyf.study.mapper.db1",sqlSessionFactoryRef="db1SqlSessionFactory")publicclassDb1DataSourceConfig{@Bean("db1DataSource")@ConfigurationProperties(prefix="spring.datasource.db1")//读取application.yml中的配置参数映射成为一个对象publicDataSourcegetDb1DataSource(){returnDataSourceBuilder.create().build();}@Bean("db1SqlSessionFactory")publicSqlSessionFactorydb1SqlSessionFactory(@Qualifier("db1DataSource")DataSourcedataSource)throwsException{SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();bean.setDataSource(dataSource);//mapper的xml形式文件位置必须要配置,不然将报错:nostatement(这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath*:mapping/db1/*.xml"));returnbean.getObject();}@Bean("db1SqlSessionTemplate")publicSqlSessionTemplatedb1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory")SqlSessionFactorysqlSessionFactory){returnnewSqlSessionTemplate(sqlSessionFactory);}}
importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.mybatis.spring.SqlSessionTemplate;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.boot.jdbc.DataSourceBuilder;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importjavax.sql.DataSource;/***@Authorif*@Description:注意以下有些文件路径需要更改*@Date2021-05-20下午09:56*/@Configuration@MapperScan(basePackages="com.ifyyf.study.mapper.db2",sqlSessionFactoryRef="db2SqlSessionFactory")publicclassDb2DataSourceConfig{@Bean("db2DataSource")@ConfigurationProperties(prefix="spring.datasource.db2")//读取application.yml中的配置参数映射成为一个对象publicDataSourcegetDb2DataSource(){returnDataSourceBuilder.create().build();}@Bean("db2SqlSessionFactory")publicSqlSessionFactorydb2SqlSessionFactory(@Qualifier("db2DataSource")DataSourcedataSource)throwsException{SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();bean.setDataSource(dataSource);//mapper的xml形式文件位置必须要配置,不然将报错:nostatement(这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)bean.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath*:mapping/db2/*.xml"));returnbean.getObject();}@Bean("db2SqlSessionTemplate")publicSqlSessionTemplatedb2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory")SqlSessionFactorysqlSessionFactory){returnnewSqlSessionTemplate(sqlSessionFactory);}}三、测试代码运行3.1 测试类中测试代码
springboot项目中测试类进行测试
importcom.ifyyf.study.mapper.db1.Db1Mapper;importcom.ifyyf.study.mapper.db2.Db2Mapper;importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.context.SpringBootTest;importjavax.annotation.Resource;@SpringBootTestclassStudyApplicationTests{@ResourceprivateDb1Mapperdb1Mapper;@ResourceprivateDb2Mapperdb2Mapper;@TestvoidcontextLoads(){System.out.println(db1Mapper.add());System.out.println(db2Mapper.add());}}
感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot怎么配置多数据源”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。