Closed lishuanghe214 closed 4 years ago
我的没有xml @Bean //@ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // Here to config mybatis // sqlSessionFactoryBean.setTypeAliasesPackage("cn.com.hellowood.dynamicdatasource.mapper"); // sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("mappers/**Mapper.xml")); // Here is very important, if don't config this, will can't switch datasource // put all datasource into SqlSessionFactoryBean, then will autoconfig SqlSessionFactory sqlSessionFactoryBean.setDataSource(dynamicDataSource()); return sqlSessionFactoryBean; }
Switch DataSource to [slave] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] 2020-11-03 12:36:43.669 [http-nio-0.0.0.0-8001-exec-7] INFO c.w.s.dynamicdatasource.DynamicRoutingDataSource - Current DataSource is [slave] 2020-11-03 12:36:43.674 [http-nio-0.0.0.0-8001-exec-7] INFO c.w.s.dynamicdatasource.DynamicDataSourceAspect - Restore DataSource to [master] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] 控制台信息是这样,但是读不到数据
You can reference this document https://github.com/helloworlde/SpringBoot-DynamicDataSource/blob/master/Issues.md
private final String[] QUERY_PREFIX = {"list"};
控制台打印 Switch DataSource to [slave] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()] Restore DataSource to [master] in Method [List com.weiyin.streetlamp.menu.dao.MenuDao.listAll()]
但是查询的还是主库中的数据,我配置的一主一次从