In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Most people do not understand the knowledge points of this article "what is the principle and process of Mybatis implementation subcontract definition database", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Mybatis implementation subcontract definition database principle and process is what" article.
Preface
In business requirements, we need to connect two databases to process data, and we need to use dynamic data sources. By understanding the framework of mybatis, we plan to use subcontracting to distinguish between data sources.
Principle premise:
We all have four steps to use mybatis.
1: build SqlSessionFactory
2: get the sqlSession object through SqlSessionFactory
3: get the dynamic proxy object of Mapper through sqlSession object
4: get the return value by executing the dynamic proxy object
In fact, the Environment object in the Configuration object after clicking sqlSessionFactory binds our dataSource object.
Similarly, we found through debug that the mapper object after the dynamic proxy also holds the Configuration object and binds our connection information.
Conjecture
If you need to implement different data sources by subcontracting, you need to specify different sqlSessionFactroy for different mapper, and check the annotated source code of mapperScan.
By understanding annotations, we can specify the sqlSessionFactory object of the maper dynamic proxy object. As a result, basePackages can then use different sqlSessionFactory for different packages to implement different data sources for different packages.
Implementation 1: define two data sources
The two data sources are named bit ds01,ds02, which uses a database connection pool that is both druids. For brief introduction, the detailed configuration is not shown.
2: define two SqlSessionFactory
Define two sqlSessionFactory, each binding to two data sources.
3: different packages bind different sqlSessionFactory
Use @ MapperScan to specify the scanned package and notify the specified SqlSessionFactory
@ MapperScan (basePackages = "com.lenven.demo.dao.ds02", sqlSessionFactoryRef = "salveSqlSessionFactory") @ MapperScan (basePackages = "com.lenven.demo.dao.ds01", sqlSessionFactoryRef = "masterSqlSessionFactory") 4: test
The mapper tests of two different packages can query the data correctly.
Expand:
By implementing
AbstractRoutingDataSource can also implement dynamic data sources.
This class implements the interface of DataSource, and can be configured for each data source in the
@ Nullable private Map resolvedDataSources
In this attribute.
Let's get the key of the data source we need to use by exposing a determineCurrentLookupKey. But we need to pay attention to the problem of affairs.
The above is about the content of this article on "what is the principle and process of Mybatis implementing subcontract definition database". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to 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.
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.