qua
V2EX  ›  问与答

Java Service 层的业务代码应该调用这个 Service 对应 Repository 的 CRUD 方法还是这个 Service 自己的 CRUD 方法?

  •  
  •   qua · Oct 10, 2021 · 2110 views
    This topic created in 1856 days ago, the information mentioned may be changed or developed.
    public class AService {
    
        ARepository aRepository;
        
        // 复杂业务方法
        public void doSomeThing(Long id) {
        	getById(id);
            还是
            aRepository.getById(id);
            ...
            ...
        }
        
        public A getById(Long id) {...}
    }
    
    2 replies  •  2021-10-11 10:20:36 +08:00
    EscYezi
    EscYezi
    Oct 11, 2021 via iPhone
    1
    这两个不冲突,个人倾向复杂或者通用的查询由 service 自己封装(方法内部调用的还是 repository ),简单的直接调用 repository,多一层意义不大
    9c04C5dO01Sw5DNL
    9c04C5dO01Sw5DNL
    Oct 11, 2021
    2
    需要注意的是如果用了事务,用 this 会导致事务不生效。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1013 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:38 · PVG 02:38 · LAX 10:38 · JFK 13:38
    ♥ Do have faith in what you're doing.