V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
coollight56
V2EX  ›  Java

IDEA mybatis 困扰很久的 xml SQL 格式化问题,想大家帮忙验证一下

  •  
  •   coollight56 · 39 天前 · 1770 次点击
    这是一个创建于 39 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果 sql 内不包含标签格式化没问题,比如下面这样

      <select id="getShareRoleUserId" resultType="com.agt.technology.component.role.pojo.po.RoleUserItem">
            SELECT r.id,
                   ur.user_id
            FROM agt_cim_role r
                     JOIN agt_cim_user_role ur ON ur.role_id = r.id
            WHERE deleted = 0
              AND r.type = 3
        </select>
    

    但是如果包含标签的话,就会想下面一样把所有行都齐平了

    <select id="getShareRoleUserId" resultType="com.agt.technology.component.role.pojo.po.RoleUserItem">
            SELECT r.id,
            ur.user_id
            FROM agt_cim_role r
            JOIN agt_cim_user_role ur ON ur.role_id = r.id
            WHERE
            deleted = 0
            AND r.type = 3
            <if test="roleIds != null">
                AND r.id in
                <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                    #{roleId}
                </foreach>
            </if>
        </select>
    

    我试过同事的电脑也是这样,是不是我们配置出了什么问题,大佬们帮我看看你们的格式化会这样吗

    6 条回复    2025-04-28 15:19:19 +08:00
    coollight56
        1
    coollight56  
    OP
       39 天前


    一个更直观的例子 直接加个 where 标签格式化就失效了
    pweng286
        2
    pweng286  
       39 天前
    我也这样
    Belmode
        3
    Belmode  
       39 天前
    不要整个文件格式化,在 IDEA 中选中需要格式化的地方,进行局部格式化就行了。
    newaccount
        4
    newaccount  
       39 天前
    放弃自动格式化,手动弄弄就完了
    select / from / where / group by / having / order by 这些重点语句行顶头,跟<select 相比锁紧四个空格
    其余次要行缩进四个空格,比如 select 后续换行字段 / join / and / or
    放弃自动格式化的 and 缩进两空格 or 缩进三空格,统一使用四空格,按一次 tab 就完事
    自动格式化只是方便操作,手动习惯了之后写完不需要格式化
    JYii
        5
    JYii  
       39 天前
    @newaccount #4 我也是放弃格式化了,因为一些长 sql 我都是从 navicat 调好(没有 idea 付费),复制过来调调格式
    interim
        6
    interim  
       38 天前
    我用的 MyBatisCodeHelperPro 插件,格式化后缩进是正常的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5629 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 06:38 · PVG 14:38 · LAX 23:38 · JFK 02:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.