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

Springboot项目之mybatis-plus多容器分布式部署id重复问题之源码解析

mybatis-plus 3.3.2 部署多个pod id冲突问题

配置:

# 设置随机
mybatis-plus.global-config.worker-id: ${random.int(1,31)}
mybatis-plus.global-config.datacenter-id: ${random.int(1,31)}

源码解析:MybatisSqlSessionFactoryBean

重点:new MybatisSqlSessionFactoryBuilder().build(targetConfiguration); -->IdWorker.setIdentifierGenerator(identifierGenerator);

protected SqlSessionFactory buildSqlSessionFactory() throws Exception {final MybatisConfiguration targetConfiguration;// TODO 使用 MybatisXmlConfigBuilder 而不是 XMLConfigBuilderMybatisXMLConfigBuilder xmlConfigBuilder = null;if (this.configuration != null) {targetConfiguration = this.configuration;if (targetConfiguration.getVariables() == null) {targetConfiguration.setVariables(this.configurationProperties);} else if (this.configurationProperties != null) {targetConfiguration.getVariables().putAll(this.configurationProperties);}} else if (this.configLocation != null) {// TODO 使用 MybatisXMLConfigBuilderxmlConfigBuilder = new MybatisXMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);targetConfiguration = xmlConfigBuilder.getConfiguration();} else {LOGGER.debug(() -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");// TODO 使用 MybatisConfigurationtargetConfiguration = new MybatisConfiguration();Optional.ofNullable(this.configurationProperties).ifPresent(targetConfiguration::setVariables);}// TODO 无配置启动所必须的this.globalConfig = Optional.ofNullable(this.globalConfig).orElseGet(GlobalConfigUtils::defaults);this.globalConfig.setDbConfig(Optional.ofNullable(this.globalConfig.getDbConfig()).orElseGet(GlobalConfig.DbConfig::new));// TODO 初始化 id-work 以及 打印骚东西targetConfiguration.setGlobalConfig(this.globalConfig);// TODO 自定义枚举类扫描处理if (hasLength(this.typeEnumsPackage)) {Set<Class<?>> classes;if (typeEnumsPackage.contains(StringPool.STAR) && !typeEnumsPackage.contains(StringPool.COMMA)&& !typeEnumsPackage.contains(StringPool.SEMICOLON)) {classes = scanClasses(typeEnumsPackage, null);if (classes.isEmpty()) {LOGGER.warn(() -> "Can't find class in '[" + typeEnumsPackage + "]' package. Please check your configuration.");}} else {classes = new HashSet<>();String[] typeEnumsPackageArray = tokenizeToStringArray(this.typeEnumsPackage,ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);Assert.notNull(typeEnumsPackageArray, "not find typeEnumsPackage:" + typeEnumsPackage);Stream.of(typeEnumsPackageArray).forEach(typePackage -> {try {Set<Class<?>> scanTypePackage = scanClasses(typePackage, null);if (scanTypePackage.isEmpty()) {LOGGER.warn(() -> "Can't find class in '[" + typePackage + "]' package. Please check your configuration.");} else {classes.addAll(scanTypePackage);}} catch (IOException e) {throw new MybatisPlusException("Cannot scan class in '[" + typePackage + "]' package", e);}});}// 取得类型转换注册器TypeHandlerRegistry typeHandlerRegistry = targetConfiguration.getTypeHandlerRegistry();classes.stream().filter(Class::isEnum).filter(MybatisEnumTypeHandler::isMpEnums).forEach(cls -> typeHandlerRegistry.register(cls, MybatisEnumTypeHandler.class));}Optional.ofNullable(this.objectFactory).ifPresent(targetConfiguration::setObjectFactory);Optional.ofNullable(this.objectWrapperFactory).ifPresent(targetConfiguration::setObjectWrapperFactory);Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl);if (hasLength(this.typeAliasesPackage)) {scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream().filter(clazz -> !clazz.isAnonymousClass()).filter(clazz -> !clazz.isInterface()).filter(clazz -> !clazz.isMemberClass()).forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias);}if (!isEmpty(this.typeAliases)) {Stream.of(this.typeAliases).forEach(typeAlias -> {targetConfiguration.getTypeAliasRegistry().registerAlias(typeAlias);LOGGER.debug(() -> "Registered type alias: '" + typeAlias + "'");});}if (!isEmpty(this.plugins)) {Stream.of(this.plugins).forEach(plugin -> {targetConfiguration.addInterceptor(plugin);LOGGER.debug(() -> "Registered plugin: '" + plugin + "'");});}if (hasLength(this.typeHandlersPackage)) {scanClasses(this.typeHandlersPackage, TypeHandler.class).stream().filter(clazz -> !clazz.isAnonymousClass()).filter(clazz -> !clazz.isInterface()).filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())).forEach(targetConfiguration.getTypeHandlerRegistry()::register);}if (!isEmpty(this.typeHandlers)) {Stream.of(this.typeHandlers).forEach(typeHandler -> {targetConfiguration.getTypeHandlerRegistry().register(typeHandler);LOGGER.debug(() -> "Registered type handler: '" + typeHandler + "'");});}if (!isEmpty(this.scriptingLanguageDrivers)) {Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {targetConfiguration.getLanguageRegistry().register(languageDriver);LOGGER.debug(() -> "Registered scripting language driver: '" + languageDriver + "'");});}Optional.ofNullable(this.defaultScriptingLanguageDriver).ifPresent(targetConfiguration::setDefaultScriptingLanguage);if (this.databaseIdProvider != null) {// fix #64 set databaseId before parse mapper xmlstry {targetConfiguration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));} catch (SQLException e) {throw new NestedIOException("Failed getting a databaseId", e);}}Optional.ofNullable(this.cache).ifPresent(targetConfiguration::addCache);if (xmlConfigBuilder != null) {try {xmlConfigBuilder.parse();LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'");} catch (Exception ex) {throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);} finally {ErrorContext.instance().reset();}}targetConfiguration.setEnvironment(new Environment(MybatisSqlSessionFactoryBean.class.getSimpleName(),this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory,this.dataSource));if (this.mapperLocations != null) {if (this.mapperLocations.length == 0) {LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found.");} else {for (Resource mapperLocation : this.mapperLocations) {if (mapperLocation == null) {continue;}try {XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());xmlMapperBuilder.parse();} catch (Exception e) {throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);} finally {ErrorContext.instance().reset();}LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'");}}} else {LOGGER.debug(() -> "Property 'mapperLocations' was not specified.");}final SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(targetConfiguration);// TODO SqlRunnerSqlHelper.FACTORY = sqlSessionFactory;// TODO 打印骚东西 Bannerif (globalConfig.isBanner()) {System.out.println(" _ _   |_  _ _|_. ___ _ |    _ ");System.out.println("| | |\\/|_)(_| | |_\\  |_)||_|_\\ ");System.out.println("     /               |         ");System.out.println("                        " + MybatisPlusVersion.getVersion() + " ");}return sqlSessionFactory;
}

idworker实战

其中mybatis-plus内置的ImadcnIdentifierGenerator方法,就已经提供了对idworker框架的支持。

升级版本:v3.4.0

  " + MybatisPlusVersion.getVersion() + " ");
}return sqlSessionFactory;

}


# idworker实战其中mybatis-plus内置的`ImadcnIdentifierGenerator`方法,就已经提供了对idworker框架的支持。升级版本:v3.4.0![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/6aac34ebf0f36867d15d889d56f55fd7.png)

相关文章:

  • 【生成对抗网络GAN】一篇文章讲透~
  • 《无名之辈》天涯镖局攻略:高效拉镖窍门!
  • Codeup_1132:问题 A: 最长公共子序列
  • 大话设计模式之模板方法模式
  • 云原生最佳实践系列 4:基于 MSE 和 SAE 的微服务部署与压测
  • 你的 Python 代码需要解释一下了!
  • ideaSSM 人才引进管理系统bootstrap开发mysql数据库web结构java编程计算机网页源码maven项目
  • 医院同步时钟系统的耐用性与可靠性
  • 【数据分享】1929-2023年全球站点的逐日平均海平面压力(Shp\Excel\免费获取)
  • git提交和回退
  • 【前端】Layui的表格常用功能,表单提交事件,表格下拉按钮点击事件,表格外的按钮点击事件
  • dfs (蓝桥备赛)
  • 01.ArcEngine中IField的属性详细描述
  • 程序员也写歌啦
  • 什么是数据湖
  • $translatePartialLoader加载失败及解决方式
  • [译]如何构建服务器端web组件,为何要构建?
  • CODING 缺陷管理功能正式开始公测
  • CSS中外联样式表代表的含义
  • ECMAScript6(0):ES6简明参考手册
  • HomeBrew常规使用教程
  • Netty源码解析1-Buffer
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 分类模型——Logistics Regression
  • 前端面试题总结
  • 算法系列——算法入门之递归分而治之思想的实现
  • 项目实战-Api的解决方案
  • 延迟脚本的方式
  • Nginx实现动静分离
  • Prometheus VS InfluxDB
  • #Linux(权限管理)
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (4)STL算法之比较
  • (9)目标检测_SSD的原理
  • (八)Flask之app.route装饰器函数的参数
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (小白学Java)Java简介和基本配置
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)linux下的时间函数使用
  • (转)ObjectiveC 深浅拷贝学习
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .apk文件,IIS不支持下载解决
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .NET中 MVC 工厂模式浅析
  • @Autowired注解的实现原理
  • @DataRedisTest测试redis从未如此丝滑
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • @JsonFormat与@DateTimeFormat注解的使用
  • [1525]字符统计2 (哈希)SDUT
  • [cogs2652]秘术「天文密葬法」
  • [JavaEE] 线程与进程的区别详解