V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
reavid
V2EX  ›  Java

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

  •  
  •   reavid · Dec 4, 2025 · 2620 views
    This topic created in 149 days ago, the information mentioned may be changed or developed.

    刚刚 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
       Dec 4, 2025
    有人喜欢,有人讨厌,不做评论
    itechify
        2
    itechify  
    PRO
       Dec 4, 2025   ❤️ 1
    更感觉是代码规范问题,数据库持久层写在 Repository/DAO 里面吧
    codingerj
        3
    codingerj  
       Dec 4, 2025
    我遇到过挺多这样写代码的人,给我的感觉就是会这么写的这辈子都写不出优雅易读的代码,干这行纯是混口饭吃
    reavid
        4
    reavid  
    OP
       Dec 4, 2025
    @itechify #2 也可以直接写在 xml 里面呀,没必要特意再封一层吧。
    fj19
        5
    fj19  
       Dec 4, 2025
    你写在 dao 层怎么了,要啥 xml
    bjfane
        6
    bjfane  
    PRO
       Dec 4, 2025
    不懂就问,mybatis-plus 这么写能不能平滑切换 mysql 和 postgre ? 如果是 sql 的话 有的是不是不行?
    v2306
        7
    v2306  
       Dec 4, 2025
    Ketteiron
        8
    Ketteiron  
       Dec 4, 2025
    这段代码确实很垃圾,明明一行链式就搞定。
    mybatis-plus 我觉得没什么问题,在复杂动态条件拼接与类型安全上远胜 xml 。
    但受困于 java 的表达能力,写起来确实有点折磨。
    slert
        9
    slert  
       Dec 4, 2025
    java 就没有好点的 orm 了吗?
    imesrdfi8dzs
        10
    imesrdfi8dzs  
       Dec 5, 2025
    单纯是这段代码写得废话多而已。和 ORM 无关。
    这种老哥写 sql 也更是灾难。
    kerwin1874
        11
    kerwin1874  
       Dec 5, 2025
    我是反过来了,看 xml sql 看得头大,单表 crud 用 mybatis-plus 确实好使,不想在 service 写 wrapper 那就再开一层封装起来
    florentino
        12
    florentino  
       Dec 5, 2025
    垃圾 sql, 狗都不碰, 如果有什么更好的表意语言,我觉得可以完全抛弃 sql , 希望 Ai 早日可以用自然语言代替 sql 语句, 另外这段代码比 xml 的表达更直接,看一眼就知道在干嘛, 而且 idea 对于 xml 的格式化能力太弱了,虽然写的没问题,但是还是会给你红色高亮
    looveh
        13
    looveh  
       Dec 5, 2025
    我这边看到一个 Controller ,一个方法近 200 行,起码有 150 行写的 wrapper.xxx()👴
    finolaire
        14
    finolaire  
       Dec 9, 2025
    @codingerj 急了急了,你妈生了你这个狗杂种,哈哈哈哈,狗杂种你全家怎么还没被车撞死,这个是你的 QQ 吧,哈哈哈
    yxisenx
        15
    yxisenx  
       Dec 9, 2025
    @livid #14 骂人,他的历史回复也有挺多骂人的
    Livid
        16
    Livid  
    MOD
    PRO
       Dec 9, 2025
    @yxisenx 谢谢。14 楼的账号已经被彻底 ban 。
    xxbern
        17
    xxbern  
       Dec 10, 2025
    这种查询代码可以放到 @mapper 接口 写成 default 方法,还有这种 service 一个萝卜一个坑的接口和实现,确实是规范问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   927 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 18:37 · PVG 02:37 · LAX 11:37 · JFK 14:37
    ♥ Do have faith in what you're doing.