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

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

  •  
  •   qua · Oct 10, 2021 · 1756 views
    This topic created in 1672 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
        1
    EscYezi  
       Oct 11, 2021 via iPhone
    这两个不冲突,个人倾向复杂或者通用的查询由 service 自己封装(方法内部调用的还是 repository ),简单的直接调用 repository,多一层意义不大
    9c04C5dO01Sw5DNL
        2
    9c04C5dO01Sw5DNL  
       Oct 11, 2021
    需要注意的是如果用了事务,用 this 会导致事务不生效。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   916 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:02 · PVG 05:02 · LAX 14:02 · JFK 17:02
    ♥ Do have faith in what you're doing.