简体   繁体   English

自动获取SQL查询中的待用列(Hibernate/Mybatis/自制)? - 创建图书馆的想法

[英]Automatically get to-be-used columns in SQL query (Hibernate/Mybatis/home-made)? - An idea to create a library

We know we should not write SELECT * ;我们知道我们不应该写SELECT * instead, should write down the columns that will indeed be used in the business logic (eg SELECT blog_title, blog_content in one query about the full article, and SELECT blog_title in another logic that only cares about titles) .相反,应该写下确实会在业务逻辑中使用的列(例如SELECT blog_title, blog_content在一个关于整篇文章的查询中的SELECT blog_title ,以及在另一个只关心标题的逻辑中的 SELECT blog_title)

However, in MyBatis or Hibernate, IMHO I have to write down those columns manually... This seems really a waste of time ...但是,在 MyBatis 或 Hibernate 中,恕我直言,我必须手动记下那些列......这似乎真的是在浪费时间......

I wonder whether there is a way to make our lives easier?我想知道是否有办法让我们的生活更轻松? More specifically:进一步来说:

  1. Is there any automatic way in Mybatis/Hibernate to do so? Mybatis/Hibernate 中是否有自动执行此操作的方法?
  2. If no, will my following naive idea works?如果不是,我下面的幼稚想法会奏效吗? (If so I am willing to create an open-source library.) (如果是这样,我愿意创建一个开源库。)

Two naive ideas (with Mybatis):两个幼稚的想法(使用 Mybatis):

a.一种。 Statically analyze the Java syntax tree, and see who uses which columns.静态分析Java语法树,看看谁用了哪些列。

b. b. Dynamically run the code once and get enough information about which columns will be used (maybe through a dynamic proxy like cglib).动态运行一次代码并获得有关将使用哪些列的足够信息(可能通过像 cglib 这样的动态代理)。

Or, we will have to manually write down those columns by ourselves, without any good aids??或者,我们将不得不自己手动记下这些列,而没有任何好的辅助工具?? :( :(

Thanks!谢谢!

Static analysis is pretty hard if your access is dynamic or in non-java files and analyzing performance problems is going to be very hard this way as well. Static 如果您的访问是动态的或在非 java 文件中,分析将非常困难,并且以这种方式分析性能问题也将非常困难。 Take a look at whatBlaze-Persistence Entity-View has to offer which is almost what you want!看看Blaze-Persistence Entity-View必须提供什么,这几乎就是您想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM