简体   繁体   English

检索数据库数据-一般检索方法?

[英]Retrieving database data - General retrieve method?

My data access classes often have a method called loadDataToEntity or something similar. 我的数据访问类通常有一个称为loadDataToEntity的方法或类似的方法。 This method takes a resultset for instance and populates an entity class which is passed on to be used by the client. 该方法以一个结果集为例,并填充一个传递给客户端使用的实体类。

This is nice because all methods which use a stored procedure to retrieve data can just pass the resultset to the loadDataToEntity method and get back an entity. 很好,因为所有使用存储过程来检索数据的方法都可以将结果集传递给loadDataToEntity方法并返回一个实体。

So it's a good way to reduce code but I often hear that you should retrieve as few columns as possible. 因此,这是减少代码的好方法,但我经常听到您应该检索尽可能少的列。 To use my general method all stored procedures needs to fetch all columns needed for loadDataToEntity(). 要使用我的通用方法,所有存储过程都需要获取loadDataToEntity()所需的所有列。 Also, when a column is added to the table and is retrieved by a stored procedure all other stored procedures needs to be altered and also retrieve that column. 同样,当将一列添加到表中并由存储过程检索时,所有其他存储过程都需要更改,并且还要检索该列。

I'm wondering if there's a better way to both achieve a general method and also retrieve different number of columns sometimes depending on the stored procedure? 我想知道是否有更好的方法既可以实现通用方法又可以有时根据存储过程来检索不同数量的列?

 
public Entity getById(int id)  
{    
    ResultSet rs;
    //call stored procedure A and stuff  
    return loadDataToEntity(rs);  
}  
public Entity getByName(String name)
{
    ResultSet rs;
    //call stored procedure B and stuff
    return loadDataToEntity(rs);
}
public Entity loadDataToEntity(ResultSet rs)
{
    // fill and return entity here
}
 

It really depends on how you've organised the different categories of entity within your database and how you're retrieving them. 这实际上取决于您如何在数据库中组织不同类别的实体,以及如何检索它们。 For example, if you have one large denormalised table that can contain many entity types with each type requiring a different combination of columns being populated then you will have to retrieve all columns if you wish to retrieve all entity types in a single query. 例如,如果您有一个大的非规范化表,其中可以包含许多实体类型,而每种类型都需要填充不同的列组合,那么如果希望在单个查询中获取所有实体类型,则必须获取所有列。

Conversely if you've organised your database schema in a normalised form you might well have a parent entity table (which might simply contain entity ID) and multiple sub-tables, one per entity type. 相反,如果您以规范化的形式组织了数据库架构,则很可能会有一个父实体表(可能只包含实体ID)和多个子表,每个子表一个。 In this case you could run multiple bespoke queries to retrieve the various entity types; 在这种情况下,您可以运行多个定制查询以检索各种实体类型。 eg 例如

select E.EntityId, S.EntityName, S.EntityAge
from Entity E
inner join SubEntity S on E.EntityId = S.EntityId

or 要么

select E.EntityId, A.EntityAddress, A.EntityGender
from Entity E
inner join AnotherSubEntity A on E.EntityId = S.EntityId

The trade-off with this second approach is the need to run multiple queries, which could in fact be slower. 使用第二种方法的权衡是需要运行多个查询,这实际上可能会更慢。 However, I've resolved this in my application by running the queries in parallel and multiplexing the results together within the application. 但是,我通过并行运行查询并在应用程序内将结果多路复用在一起来解决此问题。

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

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