In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use SpringBoot to configure multiple data sources, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use SpringBoot to configure multiple data sources. Let's take a look at it.
1. Introduction of jar package
Pom.xml file
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.6 com.multi.datasource demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot Spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.alibaba druid 1.1.8 mysql mysql-connector-java Org.projectlombok lombok 1.16.22 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin Org.projectlombok lombok 2. Properties configuration
Prepare two data sources separately
Server.port=18888mybatis.mapper-locations=classpath:mapper/*.xmlmy1.datasource.url=jdbc:mysql://10.0.0.125:3306/wyl?autoReconnect=truemy1.datasource.driverClassName=com.mysql.cj.jdbc.Drivermy1.datasource.username=rootmy1.datasource.password=123456my2.datasource.url=jdbc:mysql://10.0.0.160:3306/wyl?autoReconnect=truemy2.datasource.driverClassName=com.mysql.cj.jdbc.Drivermy2.datasource.username=rootmy2.datasource.password=1234563. Configure two data sources respectively
The first data source
Package com.multi.datasource.config;import com.alibaba.druid.pool.DruidDataSource;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.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource @ Configuration@MapperScan (basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory") public class My1DataSourceConfig {static final String PACKAGE = "com.multi.datasource.dao.my1"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; @ Value ("${my1.datasource.url}") private String url; @ Value ("${my1.datasource.username}") private String user; @ Value ("${my1.datasource.password}") private String password @ Value ("${my1.datasource.driverClassName}") private String driverClass; @ Bean (name = "my1DataSource") public DataSource my1DataSource () {DruidDataSource dataSource = new DruidDataSource (); dataSource.setDriverClassName (driverClass); dataSource.setUrl (url); dataSource.setUsername (user); dataSource.setPassword (password); dataSource.setMaxWait (Integer.MAX_VALUE); dataSource.setTestOnBorrow (true) DataSource.setTestOnReturn (true); dataSource.setTestWhileIdle (true); return dataSource;} @ Bean (name = "my1TransactionManager") public DataSourceTransactionManager my1TransactionManager () {return new DataSourceTransactionManager (my1DataSource ());} @ Bean (name = "my1SqlSessionFactory") public SqlSessionFactory my1SqlSessionFactory (@ Qualifier ("my1DataSource") DataSource my1DataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (my1DataSource) SessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (My1DataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject ();}}
The second data source
Package com.multi.datasource.config;import com.alibaba.druid.pool.DruidDataSource;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.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource @ Configuration@MapperScan (basePackages = My1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "my1SqlSessionFactory") public class My1DataSourceConfig {static final String PACKAGE = "com.multi.datasource.dao.my1"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; @ Value ("${my1.datasource.url}") private String url; @ Value ("${my1.datasource.username}") private String user; @ Value ("${my1.datasource.password}") private String password @ Value ("${my1.datasource.driverClassName}") private String driverClass; @ Bean (name = "my1DataSource") public DataSource my1DataSource () {DruidDataSource dataSource = new DruidDataSource (); dataSource.setDriverClassName (driverClass); dataSource.setUrl (url); dataSource.setUsername (user); dataSource.setPassword (password); dataSource.setMaxWait (Integer.MAX_VALUE); dataSource.setTestOnBorrow (true) DataSource.setTestOnReturn (true); dataSource.setTestWhileIdle (true); return dataSource;} @ Bean (name = "my1TransactionManager") public DataSourceTransactionManager my1TransactionManager () {return new DataSourceTransactionManager (my1DataSource ());} @ Bean (name = "my1SqlSessionFactory") public SqlSessionFactory my1SqlSessionFactory (@ Qualifier ("my1DataSource") DataSource my1DataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (my1DataSource) SessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (My1DataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject ();}} 4. Dao directory
In order to distinguish the two data sources, different directories are set up.
Package com.multi.datasource.dao.my1;import com.multi.datasource.entity.UserEntity;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface Test1Mapper {UserEntity query ();} package com.multi.datasource.dao.my2;import com.multi.datasource.entity.UserEntity;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface Test2Mapper {UserEntity query ();} 5. Entitypackage com.multi.datasource.entity;import lombok.Data;@Datapublic class UserEntity {private String userName } 6. Mapper file
Query from my1 data source
Select user_name as userName from t_user
Query from my2 data source
Select user_name as userName from t_user 7. Controller Test package com.multi.datasource.controller;import com.multi.datasource.dao.my1.Test1Mapper;import com.multi.datasource.dao.my2.Test2Mapper;import com.multi.datasource.entity.UserEntity;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestControllerpublic class TestController {@ Resource private Test1Mapper test1Mapper; @ Resource private Test2Mapper test2Mapper @ RequestMapping ("query") public void query () {UserEntity user1 = test1Mapper.query (); System.out.println ("my1 dataSource:" + user1); UserEntity user2 = test2Mapper.query (); System.out.println ("my2 dataSource:" + user2);}}
Two data sources, corresponding to zhangsan and lisi, respectively.
8. Result verification
Visit http://localhost:18888/query and the result is as follows
This is the end of the article on "how to configure multiple data sources using SpringBoot". Thank you for reading! I believe you all have a certain understanding of "how to use SpringBoot to configure multiple data sources". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 247
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.