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

编程入门 行业动态 更新时间:2024-10-24 04:35:17

动态数据源自<a href=https://www.elefans.com/category/jswz/34/1771289.html style=定义SqlSessionFactoryBean时mybatis plus配置失效"/>

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

环境:

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

yaml配置:

spring:datasource:db100:username: xxxpassword: xxxjdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/100driver-class-name: com.kingbase8.Driver# url: jdbc:postgresql://xxx.xxx.xxx.xxx:54321/100?serverTimezone=Asia/Shanghai&useSSL=false# driverClassName: org.postgresql.Driverdb101:username: xxxpassword: xxxjdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/101driver-class-name: com.kingbase8.Driverdb104:username: xxxpassword: xxxjdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/104driver-class-name: com.kingbase8.Drivermybatis-plus:mapper-locations: classpath*:**mapper/*.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

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

这是因为我们自定义SqlSessionFactoryBean

    @Beanpublic MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dynamicDataSource);return sqlSessionFactoryBean;}

解决方式:

    @Bean@Scope("prototype")@ConfigurationProperties(prefix = "mybatis-plus.global-config")public GlobalConfig globalConfig(){return new GlobalConfig();}@Bean//@Scope("prototype")@ConfigurationProperties(prefix = "mybatis-plus.configuration")public MybatisConfiguration mybatisConfiguration(){return new MybatisConfiguration();}

SqlSessionFactoryBean修改:

@MapperScan(basePackages = "com.etoak.wsdhla.mapper", sqlSessionFactoryRef = "dynamicDataSourceSqlSessionFactory")
@Configuration
public class DataSourceConfig {@Bean@Primary@ConfigurationProperties(prefix = "spring.datasource.db100")public DataSource dataSource100() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.db101")public DataSource dataSource101() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.db104")public DataSource dataSource104() {return DataSourceBuilder.create().build();}@Bean@Scope("prototype")@ConfigurationProperties(prefix = "mybatis-plus.global-config")public GlobalConfig globalConfig(){return new GlobalConfig();}@Bean//@Scope("prototype")@ConfigurationProperties(prefix = "mybatis-plus.configuration")public MybatisConfiguration mybatisConfiguration(){return new MybatisConfiguration();}/*** 将动态代理数据源对象放入Spring容器中*/@Beanpublic DynamicDataSource dynamicDataSource(@Qualifier("dataSource100") DataSource dataSource100, @Qualifier("dataSource101") DataSource dataSource101, @Qualifier("dataSource104") DataSource dataSource104) {// 这个地方是比较核心的targetDataSource 集合是我们数据库和名字之间的映射Map<Object, Object> targetDataSource = new HashMap<>();targetDataSource.put("db100", dataSource100);targetDataSource.put("db101", dataSource101);targetDataSource.put("db104", dataSource104);DynamicDataSource dataSource = new DynamicDataSource();// 设置所有的数据源dataSource.setTargetDataSources(targetDataSource);// 设置默认使用的数据源对象dataSource.setDefaultTargetDataSource(dataSource100);return dataSource;}@Beanpublic MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dynamicDataSource);// 设置数据库mapper的xml文件路径sqlSessionFactoryBean .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));sqlSessionFactoryBean.setConfiguration(mybatisConfiguration());sqlSessionFactoryBean.setGlobalConfig(globalConfig());return sqlSessionFactoryBean;}
}

更多推荐

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

本文发布于:2023-12-07 14:03:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1671383.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   动态   数据   mybatis   SqlSessionFactoryBean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!