当前位置: 首页 > news >正文

动态数据源自定义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;}
}

http://www.lryc.cn/news/196612.html

相关文章:

  • 【Qt控件之QDialogButtonBox】概述及使用
  • IPv6知识概述 - ND协议
  • react-redux的connect函数实现
  • Vue3使用Vite创建项目
  • NCV7724DQBR2G车规级半桥电机驱动芯片-专为汽车,工业自动化应用提供完美解决方案
  • NSS [GWCTF 2019]枯燥的抽奖
  • 微信小程序会议OA系统
  • CICD:Circle CI 实现CICD
  • 竞赛 深度学习YOLO安检管制物品识别与检测 - python opencv
  • 【华为OD机试python】斗地主之顺子【2023 B卷|100分】
  • ant design DatePicker禁用之前的时间
  • C语言---预处理详解
  • 数组和对象有什么区别?
  • 顺序表(第二节)实现和解析
  • Hadoop3教程(二十一):MapReduce中的压缩
  • 04、RocketMQ -- 核心基础使用
  • mysql中date/datetime类型自动转go的时间类型time.Time
  • MATLAB算法实战应用案例精讲-【图像处理】机器视觉(基础篇)
  • LDAP协议工作原理
  • 【Jetpack Compose】BOM是什么?
  • 多域名SSL数字证书是什么呢
  • 杭电oj--求奇数的乘积
  • E053-web安全应用-Brute force暴力破解初级
  • 外汇天眼;VT Markets 赞助玛莎拉蒂MSG Racing电动方程式世界锦标赛
  • 使用vscode + vite + vue3+ element3 搭建vue3脚手架
  • 竞赛 深度学习+opencv+python实现车道线检测 - 自动驾驶
  • spring boot 下载resources下的静态文件为流格式
  • HTML渲染过程
  • [已解决]llegal target for variable annotation
  • nodejs基于vue小型企业银行账目管理系统