Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[错误报告]: mybatis-plus-generator 升级到 3.5.10.1 后,entity字段注解顺序不一致 #6685

Open
3 tasks done
coder-xiaomo opened this issue Jan 24, 2025 · 3 comments

Comments

@coder-xiaomo
Copy link
Contributor

coder-xiaomo commented Jan 24, 2025

确认

  • 我使用的版本是最新版, 并且使用插件确认过项目里无依赖版本冲突
  • 我已经在 issue 中搜索过, 确认问题没有被提出过
  • 我已经修改标题, 将标题中的 描述 替换为遇到的问题

当前程序版本

mybatis-plus-generator 3.5.10.1

问题描述

本 issue 与 #6682 一致


从以前的版本升级上来后,运行这个 generator 生成的 entity 注解的先后顺序不统一,会导致git历史出现很多修改。

如图,左侧为 3.5.9 版本生成的 entity(先 @Schema,后 @TableField),右侧为 3.5.10.1 版本生成的(@Schema,@TableField 前后顺序不一致)。

Image

个人认为,此处排序应该

  • 方案1:按照注解的首字母排序(例如 @Schema @TableField 按照 s < t 排序)
  • 方案2:按照注解名的长度排序(例如 @Schema @TableField 按照 "@Schema".length (6) < "@TableField".length (10) 排序)

从结果来看,似乎当前是按照整行代码的长度进行排序,就导致了@Schema@TableField 两者哪一行短哪行就在前面。

详细堆栈日志

试图阅读了一下源码,找到了这里(但这里似乎是针对entity类名上的注解排序,暂时还没有找到具体entity字段的注解排序的逻辑):

https://github.com/baomidou/mybatis-plus/blame/c9ec9569230b1ed34ab44d992107d8c1b7c588cd/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/Entity.java#L428
@nieqiurong
Copy link
Contributor

以字符长度

@coder-xiaomo
Copy link
Contributor Author

以字符长度

@nieqiurong 是的,就是说目前以字符长度或许不是最佳的排序方式。

按照注解的首字母 A-Z 排序,或者按照注解的注解名(不包含后面括号参数部分)的长度排序会更合适 。

这样不同字段的注解可以确保是统一顺序的,而且如果后续修改字段备注,也不会出现注解顺序前后变化的问题(同时也可以确保跟旧版的 generator 保持基本一致)

或者至少,给一个可以配置的配置项,或者能够 MybatisPlusConfig 配置类重写这个排序规则也可以,目前没办法修改这个配置

@nieqiurong
Copy link
Contributor

entityBuilder()
            .annotationAttributesFunction(annotationAttributes -> annotationAttributes.stream()
                .sorted(Comparator.comparingInt((AnnotationAttributes c) -> c.getDisplayName().charAt(1))).collect(Collectors.toList()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants