当前位置:网站首页>Guns framework multi data source configuration without modifying the configuration file

Guns framework multi data source configuration without modifying the configuration file

2022-06-12 05:58:00 The stars can count people

Project requirements , Configure multiple data sources , I also found a lot on the Internet , But there are many configuration items , and Guns It comes with multiple data source facets AOP, Didn't use it . I interrupted and looked at the configuration , Found a solution :

  1. stay database_info Insert information about the new data source into the table
     Insert picture description here
  2. Annotate the methods that need to switch data sources @DataSource(name = “dataSourceBiz”)
@Override
@DataSource(name = "dataSourceBiz")
public List<SysAlarm> getAll() {
    
System.out.println(DataSourceContextHolder.getDataSourceType());
    return this.baseMapper.selectList(null);
}

name The value of is written in the database db_name field value

  1. result
    Query results of the master data source :
     Insert picture description here
    Query results of the second data source :
     Insert picture description here
    The output is DataSourceContextHolder.getDataSourceType(), That's the data source

Guns The framework has written the switching of multiple data sources , So we don't have to write our own configuration , The configuration of the framework itself is as follows :

1. DataSourceConfig class

 Insert picture description here
The project will start with DataSourceConfig Class , Configure the master data source , Namely application-local.yml or dev.yml Database configured in ,prefix The mapping is yml Inside spring.datasource

then , Create a master database instance
 Insert picture description here
After a series of database operations , Will enter the following :
 Insert picture description here
The corresponding is MultiSourceExAop class

2. SqlSessionFactoryConfig class

Then go into this class , Create the primary database sqlsessionfactory
 Insert picture description here
Initialize the master data source container and other data sources sqlsessionfactory Containers
 Insert picture description here
Within this method initBaseSqlSessionFactory Method will call all data sources in the database , except yml Master data source configured in , Other data is initialized in this method

3. DataSourceContext class

The initialization container related methods called in the multi data source switching template just mentioned are in this class
 Insert picture description here

4. MultiSourceExAop class

Switch the aspect of the data source , Scanning to @DataSource Triggered on annotation
 Insert picture description here

原网站

版权声明
本文为[The stars can count people]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120554000899.html