avk458
V2EX  ›  Java

最近一个项目又用上了 Hibernate,请问在这样的情况下如何优雅的查询

  •  
  •   avk458 · Aug 9, 2019 · 4067 views
    This topic created in 2493 days ago, the information mentioned may be changed or developed.

    现在接手的一个项目比较老,前面经历了 3 个开发团队,业务逻辑复杂表关系设计也比较混乱。 现在需要开发一个接口,需要的数据要从很多有关系没关系的表中取,如果写 SQL 大致如下

    select *
    from t_test_object tto
    left join t_consign_info tci on tto.consign_info_id = tci.id
    left join t_consign_project tcp on tci.project_id = tcp.id
    left join t_report_object tro on tto.id = tro.test_object_id
    left join t_report tr on tro.report_id = tr.id
    left join t_task_report ttr on tr.id = ttr.report_id
    left join t_test_task ttt on ttr.task_id = ttt.id
    where tcp.is_test_item = '0';
    

    还有几张表没有写进去,接口要求支持分页,看以前的类似接口都是写的这种 SQL 查询的。 学了一下criteriahibernatetemplate之后,想请教一下这种业务需求是将数据的拼接以面向对象的思想放到代码中好,还是用 SQL 查询将压力放到数据库更好呢?

    2 replies    2019-08-09 17:32:52 +08:00
    ayonel
        1
    ayonel  
       Aug 9, 2019
    看这段 sql 的性能吧,如果性能 ok,还是上 sql 省事。分页,用各种分页插件,比如 pageHelper 啥的。
    join 查询,非 sql 的 ORM 实现方式都不优雅,比直接上 sql 还难维护
    singleu
        2
    singleu  
       Aug 9, 2019 via iPhone
    explain 看效率吧。还行就 sql 咯
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2673 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:20 · PVG 18:20 · LAX 03:20 · JFK 06:20
    ♥ Do have faith in what you're doing.