Home > Back-end >  How to change the default data source in springboot mybatisplus
How to change the default data source in springboot mybatisplus

Time:12-03

@ Bean
Public SqlSessionFactory SqlSessionFactory () throws the Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean=new MybatisSqlSessionFactoryBean ();
SqlSessionFactoryBean. SetDataSource (dynamicDataSource ());
SqlSessionFactoryBean. SetMapperLocations (new PathMatchingResourcePatternResolver (.) getResources (" classpath * : com/JKSD/mapper/*. XML "));
SqlSessionFactoryBean. SetTypeAliasesPackage (com. JKSD. "model");
//import mybatis configuration
MybatisConfiguration configuration=new MybatisConfiguration ();
The configuration. SetJdbcTypeForNull (JdbcType. NULL);
The configuration. SetMapUnderscoreToCamelCase (true);
The configuration. SetCacheEnabled (false);
SqlSessionFactoryBean. SetConfiguration (configuration);
//PerformanceInterceptor (), OptimisticLockerInterceptor ()
//import global configuration
//solve the hump named failure
//sqlSessionFactoryBean. SetConfiguration (configuration ());
Return sqlSessionFactoryBean. GetObject ();
}

//modify MyBatis data
MybatisSqlSessionFactoryBean SqlSessionFactory=(MybatisSqlSessionFactoryBean) SpringContextUtils. GetBean (MybatisSqlSessionFactoryBean. Class);
Environment Environment=SqlSessionFactory. GetObject (). GetConfiguration (). GetEnvironment ();
The Field dataSourceField=environment. GetClass (). GetDeclaredField (" dataSource ");
DataSourceField. SetAccessible (true);//to skip check
DataSourceField. Set (environment, datasourceId);//modify mybatis data

An error
Org. Springframework. Beans. Factory. NoSuchBeanDefinitionException: No qualifying bean of type '. Com. Baomidou mybatisplus. The extension. Spring. MybatisSqlSessionFactoryBean 'available

Ever bigwigs use ah, want to achieve is to switch the data source after all about the operation of the database is to switch the past is not switch used in aop that thread)

CodePudding user response:

Configure multiple source not sweet?

CodePudding user response:

Multiple data sources, also should have two sets of corresponding entities,
In some cases, if a set of entities and storage, to switch at any time the data source, inflexible,

You can refer to this kind of writing:
 DriverManagerDataSource dataSource=new DriverManagerDataSource (); 
DataSource. SetDriverClassName (ConfigReader. GetConfig (" spring. The dataSource. The driver - class - the name "));
The dataSource. SetUrl (ConfigReader. GetConfig (" spring. The dataSource. Url "));
The dataSource. SetUsername (ConfigReader. GetConfig (" spring. The dataSource. The username "));
The dataSource. SetPassword (ConfigReader. GetConfig (" spring. The dataSource. Password "));
//create JdbcTemplate object, set up the data source
JdbcTemplate template=new JdbcTemplate (dataSource);
//String SQL="select * from the users";
List

CodePudding user response:

reference 1st floor can't white reply:
configure multiple source not sweet?
to achieve that kind of configuration is configured data source information page directly the whole project with the configuration of data sources, before which aop + annotation of switch is used in the current thread

CodePudding user response:

refer to the second floor water 2 reply:
data sources, also should have two sets of corresponding entities,
In some cases, if a set of entities and storage, to switch at any time the data source, inflexible,

You can refer to this kind of writing:
 DriverManagerDataSource dataSource=new DriverManagerDataSource (); 
DataSource. SetDriverClassName (ConfigReader. GetConfig (" spring. The dataSource. The driver - class - the name "));
The dataSource. SetUrl (ConfigReader. GetConfig (" spring. The dataSource. Url "));
The dataSource. SetUsername (ConfigReader. GetConfig (" spring. The dataSource. The username "));
The dataSource. SetPassword (ConfigReader. GetConfig (" spring. The dataSource. Password "));
//create JdbcTemplate object, set up the data source
JdbcTemplate template=new JdbcTemplate (dataSource);
//String SQL="select * from the users";
List
I through the modified mybatis data source to replace switch can realize the data source
  • Related