MyBatis multi-data source configuration and dynamic switching - build multiple data sources and dynamically switch

Author: Zen and the Art of Computer Programming

1 Introduction

With the development of the Internet, the rise of the mobile Internet and the increase in demand, the scale of website applications has also become larger and larger. Due to different businesses, each site will have different databases, so in the same system, multiple database connections need to be configured. MyBatis is currently one of the mainstream open source persistence layer frameworks, and its support for multiple data sources is particularly important. This article will introduce the multi-data source configuration and dynamic switching of MyBatis in detail, and realize the dynamic switching of three databases in combination with actual cases.
MyBatis is an excellent persistence layer framework, which can easily complete the operation of the database through simple XML or annotations. MyBatis configures database information through the MyBatis-config configuration file, and the MyBatis-mapper file is used to define SQL statements, and map these statements to Java objects through the MyBatis API, so as to achieve the separation of business logic and data access.
MyBatis supports the configuration of multiple data sources. When there are multiple data sources in a MyBatis project, you can set different dataSource attribute values ​​so that MyBatis can connect to different databases. But in general, we hope that the system can dynamically select the database to be used according to business needs during operation, that is, realize dynamic switching of databases. For example, at a certain moment, the system needs to use another database on another server. At this time, real-time switching of the database can be realized through dynamic switching. In this article, we will introduce how MyBatis realizes the configuration and dynamic switching of multiple databases based on actual cases.

2. Explanation of basic concepts and terms

Data Source (DataSource)

Data Source (DataSource) refers to the data storage location or service, which is a collection object in the database, including a set of tables used to store data, relational database management system (RDBMS)

Guess you like

Origin blog.csdn.net/universsky2015/article/details/131971082