Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of SpringBoot Multi-data Source Integration

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of the method of SpringBoot multi-source integration, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this SpringBoot multi-source integration method article. Let's take a look at it.

First, the use of multiple data sources and disadvantages 1. Scene

Business system cross-database

Data rollover (this is so low now that it should not be used at all)

System integration

two。 Malpractice

Cross-database business transaction problem

Service and dao cannot repeatedly inject data sources.

Second, use step 1. Import com.baomidou dynamic-datasource-spring-boot-starter 3.3.62 into the library. Multiple data source profile

# data source configuration

Spring:

Application:

Name: olap

Datasource:

Dynamic:

Primary: target

Target:

Jdbc-url: jdbc:mysql://192.168.1.101:15051/db1?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8

Username: root

Password: 123

Driver-class-name: com.mysql.jdbc.Driver

Source1:

Jdbc-url: jdbc:mysql://192.168.102:15052/db2?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8

Username: admin

Password: 456

Driver-class-name: com.mysql.jdbc.Driver

3. Multiple data source configuration class

Data Source 1

Import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;import javax.sql.DataSource;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.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 org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;@Configuration@MapperScan (basePackages = "com.easylinkin.dc.mapper.source1", sqlSessionFactoryRef = "source1SqlSessionFactory") public class Source1DataSourceConfig {/ / mapper.xml address private static final String MAPPER_LOCATION = "classpath*:mapper/business/source1/*.xml" / * data Source * / @ Bean (name = "source1DataSource") @ ConfigurationProperties (prefix = "spring.datasource.source1") public DataSource dataSource () {DataSource build = DataSourceBuilder.create () .build (); return build;} / * * transaction Manager * / @ Bean (name = "source1TransactionManager") public PlatformTransactionManager dataSourceTransactionManager (@ Qualifier ("source1DataSource") DataSource dataSource) {return new DataSourceTransactionManager (dataSource) } / * * session Factory * / @ Bean (name = "source1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory (@ Qualifier ("source1DataSource") DataSource dataSource) throws Exception {/ * final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource); sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (Source1DataSourceConfig.MAPPER_LOCATION)) * / / instead of using the following method in BaseMapper provided by mybatis-plus, you need to increase the mybatis-plus extension dependency MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean (); bean.setDataSource (dataSource); / / if you don't inject paging plug-ins here, it will fail / / bean.setPlugins (new PaginationInterceptor [] {new PaginationInterceptor ()}); return bean.getObject ();}}

Data Source 2

Import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;import javax.sql.DataSource;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.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.context.annotation.Primary Import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;@Configuration@MapperScan (basePackages = "com.easylinkin.dc.mapper.target", sqlSessionFactoryRef = "targetSqlSessionFactory") public class TargetDataSourceConfig {/ / mapper.xml address private static final String MAPPER_LOCATION = "classpath*:mapper/business/target/*.xml" / * main data source, the Primary annotation must be added, which indicates that the data source is the default data source project. Other data sources may also exist in the project. If the name is not specified when the data source is obtained, the data source is obtained by default. If you do not add Error * / @ Primary @ Bean (name = "targetDataSource") @ ConfigurationProperties (prefix = "spring.datasource.target") public DataSource dataSource () {DataSource build = DataSourceBuilder.create () .build () Return build;} / * transaction manager, Primary annotations function as above * / @ Bean (name = "masterTransactionManager") @ Primary public PlatformTransactionManager dataSourceTransactionManager (@ Qualifier ("targetDataSource") DataSource dataSource) {return new DataSourceTransactionManager (dataSource) } / * session factory, Primary annotation function is the same as above * / @ Bean (name = "targetSqlSessionFactory") @ Primary public SqlSessionFactory sqlSessionFactory (@ Qualifier ("targetDataSource") DataSource dataSource) throws Exception {/ * final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean (); sessionFactoryBean.setDataSource (dataSource); sessionFactoryBean.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (TargetDataSourceConfig.MAPPER_LOCATION)); return sessionFactoryBean.getObject () * / / instead of using the following method in BaseMapper provided by mybatis-plus, you need to increase the mybatis-plus extension dependency MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean (); bean.setDataSource (dataSource); / / if you don't inject paging plug-ins here, it will fail / / bean.setPlugins (new PaginationInterceptor [] {new PaginationInterceptor ()}); return bean.getObject ();}}

Note that dao must be under the package set by @ MapperScan annotation

Note that the xml file should be under the path of MAPPER_LOCATION=

Do not use @ MapperScan annotations for springBoot startup classes

For other SqlSessionFactory methods of the configuration class, see the notes.

4. Use import com.baomidou.dynamic.datasource.annotation.DS;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.metadata.IPage;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.easylinkin.dc.common.exceptions.BusinessException;import com.easylinkin.dc.common.utils.io.OutputStreamUtil;import com.easylinkin.dc.mapper.source1.FkRegisterVisitorMapper;import com.easylinkin.dc.business.visitor.entity.FkRegisterVisitor Import com.easylinkin.dc.business.visitor.entity.vo.FkRegisterVisitorVo;import com.easylinkin.dc.business.visitor.service.FkRegisterVisitorService;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.ObjectUtils;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import org.springframework.util.Assert;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException Import java.io.OutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;/** * FkRegisterVisitor table service implementation class * * @ author CodeGenerator * @ date 2022-05-16 * / @ DS ("source1") @ Slf4j@Service ("fkRegisterVisitorService") public class FkRegisterVisitorServiceImpl extends ServiceImpl implements FkRegisterVisitorService {}

Note that the service implementation class is annotated with @ DS ("data source name"), and the data source name is the same as under datasource in the configuration file.

Note that dao can no longer use @ DS annotation, whether it is reporting an error.

This is the end of this article on "the method of SpringBoot Multi-data Source Integration". Thank you for reading! I believe you all have a certain understanding of the "method of SpringBoot multi-source integration". 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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report