• 动态数据源自定义SqlSessionFactoryBean时mybatis plus配置失效


    环境:

    • 动态数据源
    • spring-boot 2.7.15
    • mybatis-plus 3.5.2

    yaml配置:

    1. spring:
    2. datasource:
    3. db100:
    4. username: xxx
    5. password: xxx
    6. jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/100
    7. driver-class-name: com.kingbase8.Driver
    8. # url: jdbc:postgresql://xxx.xxx.xxx.xxx:54321/100?serverTimezone=Asia/Shanghai&useSSL=false
    9. # driverClassName: org.postgresql.Driver
    10. db101:
    11. username: xxx
    12. password: xxx
    13. jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/101
    14. driver-class-name: com.kingbase8.Driver
    15. db104:
    16. username: xxx
    17. password: xxx
    18. jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/104
    19. driver-class-name: com.kingbase8.Driver
    20. mybatis-plus:
    21. mapper-locations: classpath*:**mapper/*.xml
    22. configuration:
    23. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

    实际运行的时候,发现mybatis-plus相关的配置都失效了。

    这是因为我们自定义SqlSessionFactoryBean

    1. @Bean
    2. public MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
    3. MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    4. sqlSessionFactoryBean.setDataSource(dynamicDataSource);
    5. return sqlSessionFactoryBean;
    6. }

    解决方式:

    1. @Bean
    2. @Scope("prototype")
    3. @ConfigurationProperties(prefix = "mybatis-plus.global-config")
    4. public GlobalConfig globalConfig(){
    5. return new GlobalConfig();
    6. }
    7. @Bean
    8. //@Scope("prototype")
    9. @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    10. public MybatisConfiguration mybatisConfiguration(){
    11. return new MybatisConfiguration();
    12. }

    SqlSessionFactoryBean修改:

    1. @MapperScan(basePackages = "com.etoak.wsdhla.mapper", sqlSessionFactoryRef = "dynamicDataSourceSqlSessionFactory")
    2. @Configuration
    3. public class DataSourceConfig {
    4. @Bean
    5. @Primary
    6. @ConfigurationProperties(prefix = "spring.datasource.db100")
    7. public DataSource dataSource100() {
    8. return DataSourceBuilder.create().build();
    9. }
    10. @Bean
    11. @ConfigurationProperties(prefix = "spring.datasource.db101")
    12. public DataSource dataSource101() {
    13. return DataSourceBuilder.create().build();
    14. }
    15. @Bean
    16. @ConfigurationProperties(prefix = "spring.datasource.db104")
    17. public DataSource dataSource104() {
    18. return DataSourceBuilder.create().build();
    19. }
    20. @Bean
    21. @Scope("prototype")
    22. @ConfigurationProperties(prefix = "mybatis-plus.global-config")
    23. public GlobalConfig globalConfig(){
    24. return new GlobalConfig();
    25. }
    26. @Bean
    27. //@Scope("prototype")
    28. @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    29. public MybatisConfiguration mybatisConfiguration(){
    30. return new MybatisConfiguration();
    31. }
    32. /**
    33. * 将动态代理数据源对象放入Spring容器中
    34. */
    35. @Bean
    36. public DynamicDataSource dynamicDataSource(@Qualifier("dataSource100") DataSource dataSource100, @Qualifier("dataSource101") DataSource dataSource101, @Qualifier("dataSource104") DataSource dataSource104) {
    37. // 这个地方是比较核心的targetDataSource 集合是我们数据库和名字之间的映射
    38. Map targetDataSource = new HashMap<>();
    39. targetDataSource.put("db100", dataSource100);
    40. targetDataSource.put("db101", dataSource101);
    41. targetDataSource.put("db104", dataSource104);
    42. DynamicDataSource dataSource = new DynamicDataSource();
    43. // 设置所有的数据源
    44. dataSource.setTargetDataSources(targetDataSource);
    45. // 设置默认使用的数据源对象
    46. dataSource.setDefaultTargetDataSource(dataSource100);
    47. return dataSource;
    48. }
    49. @Bean
    50. public MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
    51. MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
    52. sqlSessionFactoryBean.setDataSource(dynamicDataSource);
    53. // 设置数据库mapper的xml文件路径
    54. sqlSessionFactoryBean .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
    55. sqlSessionFactoryBean.setConfiguration(mybatisConfiguration());
    56. sqlSessionFactoryBean.setGlobalConfig(globalConfig());
    57. return sqlSessionFactoryBean;
    58. }
    59. }

  • 相关阅读:
    springboot+vue+elementui家具销售电商管理平台java
    基于Python爬虫和K-means算法的校园微博热点话题发现系统
    cocos2dx中,将png图片打包plist图集,使用什么工具呢?
    自学黑客(网络安全)
    【SI好文翻译】铜箔表面纹理对损耗的影响:一个有效的模型(四)
    【一天一题—Day17】623. 在二叉树中增加一行
    关于linux系统can收发,以及jetson系列can收发的说明,以及SN65HVD230 CAN board和MCP2515和TJA1050的区别是什么?
    【多区域电力系统模型】三区域电力系统的LQR和模糊逻辑控制(Matlab代码实现)
    vscode 1.70变化与关注点(人工折叠/粘性滚动等)
    springmvc中异步转同步
  • 原文地址:https://blog.csdn.net/wsdhla/article/details/133880358