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";
ListCodePudding user response: