How to configure multiple data sources in SpringBoot
This article mainly introduces how to configure multiple data sources in SpringBoot, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
1. Create database table 1.1 create database db1 and database db21.2 create table db1CREATE TABLE `db1` (`id` int unsigned zerofill NOT NULL AUTO_INCREMENT, `name` varchar (50) DEFAULT NULL, `age` int unsigned zerofill DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;1.3 create table db2 in database db2
CREATE TABLE `db2` (`id` int unsigned zerofill NOT NULL AUTO_INCREMENT, `name` varchar (50) DEFAULT NULL, `age` int unsigned zerofill DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 II. Create springboot Project 2.1 pom.xml Import dependent org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java runtime Org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine junit junit test 2.2 create application.yml text Piece (choose one of the two to configure with 2.3 Recommended) server: port: 8080 # Startup Port spring: datasource: db1: # data Source 1 jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver db2: # data Source 2 jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true& CharacterEncoding=utf8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver2.3 create application.properties file (with 2.2 choose one of the two to configure) server.port=8080 spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 spring.datasource.db1.username=root spring.datasource.db1.password=root spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver spring .datasource.db2.url = jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 spring.datasource.db2.username=root spring.datasource.db2.password=root spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver2.4 create the mapper file
Personally, I put it under the mapper package and named the file casually.
The code is written randomly. It's just a test.
Import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;/** * @ Author if * @ Description: What is it * @ Date 2021-05-20 09:52 * / @ Mapperpublic interface Db1Mapper {@ Insert ("insert into db1 (name,age) values ('if',18)") int add ();} import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper / * @ Author if * @ Description: What is it * @ Date 2021-05-20 09:52 * / @ Mapperpublic interface Db2Mapper {@ Insert ("insert into db2 (name,age) values ('fi',81)") int add ();} 2.5 create config profile
Personally, I put it under the config package and named the file casually.
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.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.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource / * * @ Author if * @ Description: note that some of the following file paths need to be changed * @ Date 2021-05-20 09:56 * / @ Configuration@MapperScan (basePackages = "com.ifyyf.study.mapper.db1" SqlSessionFactoryRef = "db1SqlSessionFactory") public class Db1DataSourceConfig {@ Bean ("db1DataSource") @ ConfigurationProperties (prefix = "spring.datasource.db1") / / read the configuration parameter mapping in application.yml to an object public DataSource getDb1DataSource () {return DataSourceBuilder.create () .build () } @ Bean ("db1SqlSessionFactory") public SqlSessionFactory db1SqlSessionFactory (@ Qualifier ("db1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource) / / the xml file location of mapper must be configured, or an error will be reported: no statement (this error may also be caused by the inconsistent path between namespace and the project in mapper xml) bean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources ("classpath*:mapping/db1/*.xml")); return bean.getObject () } @ Bean ("db1SqlSessionTemplate") public SqlSessionTemplate db1SqlSessionTemplate (@ Qualifier ("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate (sqlSessionFactory);}} 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.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.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource / * * @ Author if * @ Description: note that some of the following file paths need to be changed * @ Date 2021-05-20 09:56 * / @ Configuration@MapperScan (basePackages = "com.ifyyf.study.mapper.db2" SqlSessionFactoryRef = "db2SqlSessionFactory") public class Db2DataSourceConfig {@ Bean ("db2DataSource") @ ConfigurationProperties (prefix = "spring.datasource.db2") / / read the configuration parameter mapping in application.yml to an object public DataSource getDb2DataSource () {return DataSourceBuilder.create () .build () } @ Bean ("db2SqlSessionFactory") public SqlSessionFactory db2SqlSessionFactory (@ Qualifier ("db2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean (); bean.setDataSource (dataSource) / / the xml file location of mapper must be configured, or an error will be reported: no statement (this error may also be caused by the inconsistent path between namespace and the project in mapper xml) bean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources ("classpath*:mapping/db2/*.xml")); return bean.getObject () } @ Bean ("db2SqlSessionTemplate") public SqlSessionTemplate db2SqlSessionTemplate (@ Qualifier ("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate (sqlSessionFactory);}} 3. The test code runs the test code in the test class
Test the test class in the springboot project
Import com.ifyyf.study.mapper.db1.Db1Mapper;import com.ifyyf.study.mapper.db2.Db2Mapper;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTestclass StudyApplicationTests {@ Resource private Db1Mapper db1Mapper; @ Resource private Db2Mapper db2Mapper; @ Test void contextLoads () {System.out.println (db1Mapper.add ()); System.out.println (db2Mapper.add ()) }} Thank you for reading this article carefully. I hope the article "how to configure multiple data sources in SpringBoot" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!