项目场景:
springboot+mybatis-plus项目集成多数据源
多数据源集成方案:
目前网络上关于mybatis-plus集成多数据源有两种方案,具体实现方式可参考以下链接
(1)第一种是mybatis-plus官方提供的
参考链接: https://baomidou.com/guide/dynamic-datasource.html
(2)第二种结合阿里的druid的解决方案
参考链接: https://www.cnblogs.com/hsbt2333/p/9347249.html
这个链接里有个bug,需要注意一下,再进行MyBatisPlusConfiguration.java修改的时候
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:/mapper/*/*Mapper.xml”));
这块代码的时候是必须打开的,不需要注释
使用这种方案的时候再application.yml中配置的mybatis-plus 属性是不生效的,必须在此处进行xml的加载,否则再访问的时候会报找不到对应的mapper映射
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2()));
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml"));
MybatisConfiguration configuration = new MybatisConfiguration();
//configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
sqlSessionFactory.setConfiguration(configuration);
sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
paginationInterceptor() //添加分页功能
});
//sqlSessionFactory.setGlobalConfig(globalConfiguration());
return sqlSessionFactory.getObject();
}
问题描述:
笔者采用的是mybatis-plus方案,这种方案改动量比较少,而且mybatis-plus的配置也不会失效,
但是在使用的时候@DS ,发现只在controller里生效,再service或方法里不生效,很奇怪
原因分析:
解决方案:
但是我的项目加上这个注解后,@ds注解有的地方管用,有的地方不管用,由于我这是个子系统,不用验证登陆,我直接把shiroRealm文件删除了,问题就解决了