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

能不能别用那烦人的 MyBatis-Plus 了!

  •  
  •   reavid · 3 天前 · 836 次点击

    刚刚 clone 公司的项目代码,看到一堆的 Wrapper 真的绷不住了。好好的在 xml 里面写 SQL 不好吗?非要在 Service 写那么多和业务无关的东西,代码看得我头大。

    @Override
    public int deleteByColorId(Long colorId,String userCode) {
        LambdaUpdateWrapper<ColorLibraryFile>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ColorLibraryFile::getDeletedFlag,DeletedFlagEnum.DELETE.getCode());
        updateWrapper.set(ColorLibraryFile::getDeletedBy,userCode);
        updateWrapper.set(ColorLibraryFile::getDeletedTime, new Date());
        updateWrapper.eq(ColorLibraryFile::getColorId,colorId);
        updateWrapper.eq(ColorLibraryFile::getDeletedFlag,DeletedFlagEnum.NORMAL.getCode());
       return colorLibraryFileDao.update(null,updateWrapper);
    }
    
    
    itechify
        1
    itechify  
    PRO
       3 天前
    有人喜欢,有人讨厌,不做评论
    itechify
        2
    itechify  
    PRO
       3 天前   ❤️ 1
    更感觉是代码规范问题,数据库持久层写在 Repository/DAO 里面吧
    codingerj
        3
    codingerj  
       3 天前
    我遇到过挺多这样写代码的人,给我的感觉就是会这么写的这辈子都写不出优雅易读的代码,干这行纯是混口饭吃
    reavid
        4
    reavid  
    OP
       3 天前
    @itechify #2 也可以直接写在 xml 里面呀,没必要特意再封一层吧。
    fj19
        5
    fj19  
       3 天前
    你写在 dao 层怎么了,要啥 xml
    bjfane
        6
    bjfane  
    PRO
       3 天前
    不懂就问,mybatis-plus 这么写能不能平滑切换 mysql 和 postgre ? 如果是 sql 的话 有的是不是不行?
    v2306
        7
    v2306  
       2 天前
    Ketteiron
        8
    Ketteiron  
       2 天前
    这段代码确实很垃圾,明明一行链式就搞定。
    mybatis-plus 我觉得没什么问题,在复杂动态条件拼接与类型安全上远胜 xml 。
    但受困于 java 的表达能力,写起来确实有点折磨。
    slert
        9
    slert  
       2 天前
    java 就没有好点的 orm 了吗?
    imesrdfi8dzs
        10
    imesrdfi8dzs  
       2 天前
    单纯是这段代码写得废话多而已。和 ORM 无关。
    这种老哥写 sql 也更是灾难。
    kerwin1874
        11
    kerwin1874  
       2 天前
    我是反过来了,看 xml sql 看得头大,单表 crud 用 mybatis-plus 确实好使,不想在 service 写 wrapper 那就再开一层封装起来
    florentino
        12
    florentino  
       2 天前
    垃圾 sql, 狗都不碰, 如果有什么更好的表意语言,我觉得可以完全抛弃 sql , 希望 Ai 早日可以用自然语言代替 sql 语句, 另外这段代码比 xml 的表达更直接,看一眼就知道在干嘛, 而且 idea 对于 xml 的格式化能力太弱了,虽然写的没问题,但是还是会给你红色高亮
    looveh
        13
    looveh  
       2 天前
    我这边看到一个 Controller ,一个方法近 200 行,起码有 150 行写的 wrapper.xxx()👴
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2443 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 10:54 · PVG 18:54 · LAX 02:54 · JFK 05:54
    ♥ Do have faith in what you're doing.