简体   繁体   English

从数据库获取对象列表

[英]Getting a list of objects from database

I have an object product in my application. 我的应用程序中有一个对象产品。 This object has a list of items inside it (ArrayList). 该对象内部有一个项目列表(ArrayList)。 In the database I have 2 tables: Product and Item. 在数据库中,我有2个表:Product和Item。 Items have ids of products connected with foreign keys to product id. 商品的产品ID与产品ID的外键相关。

It is quite simple to get the Product from database and then get a list of items for it. 从数据库中获取产品,然后为其获取商品列表非常简单。

How to get a list of products from the database, which will contain all the relevant items inside? 如何从数据库中获取产品列表,其中将包含所有相关项?

As far as I know, it is not efficient to call dao for every product in the list. 据我所知,为列表中的每个产品调用dao效率不高。 How can I manage the query, or object creation to make it more efficient? 如何管理查询或创建对象以使其更高效?

I am using Spring JdbcTemplate to get data from Database. 我正在使用Spring JdbcTemplate从数据库获取数据。 Currently I am getting list of products in DAO class and straight from there I call ItemDAO for every item. 目前,我正在获取DAO类的产品列表,并从那里直接为每个项目调用ItemDAO。 The database is MySQL. 该数据库是MySQL。

Update Okay, probably I was unclear in my first approach. 更新好吧,可能我不清楚第一种方法。 The goal is to get nested lists from Database and how to make it performance-wise. 目的是从数据库中获取嵌套列表,以及如何使其性能有效。

public class Product {
    private Integer id;
    private String name;
    private String description;
    private ArrayList<Item> items;
    private Timestamp timeAdded;
}

public class Item {
    private String name;
    private Float price;
    private String type;
    private Integer productId;
}

public ArrayList getProducts(String searchName) throws SQLException {
    ArrayList<Product> products = new ArrayList<Product>();
    String sql = "SELECT * FROM product WHERE name LIKE '%" + searchName + "%'";
    List<Map> rows = getJdbcTemplate().queryForList(sql);
    for (Map row : rows) {
        Product product = setNewProduct(row);
        products.add(product);
    }
    return products;
}

private Product setNewProduct(Map row) {
    Product product = new Product();
    product.setId((Integer) row.get("id"));
    product.setName((String) row.get("name"));
    product.setDescription((String) row.get("description"));
    ItemsDao itemsDao = (ItemsDao) appContext.getBean("itemsDao");
    ArrayList<Item> items = itemsDao.getItems(product.getId());
    if (items.size() > 0) {
        product.setItems(items);
    }
    product.setTimeAdded(new java.util.Date(((Timestamp) row.get("time_added")).getTime()));
}

In ItemsDao class I just get a list of items for the product, based on the ID sent. 在ItemsDao类中,我仅基于发送的ID获取产品的项目列表。

So the question is how to call list of products and get list of items inside every product? 那么问题是如何调用产品清单并获取每个产品中的物品清单? (maybe in one complex SQL statement?). (也许在一个复杂的SQL语句中?)。 Now the performance is horrible, because when there are 30 products with 10 items each, to get the whole list of products it will have to call database so many times. 现在的性能太糟糕了,因为当有30种产品(每个产品有10个项目)时,要获得整个产品列表,必须调用数据库很多次。

The answer is that you can't when using an ORM, they all perform like crap when you have any amount of data. 答案是,使用ORM时您无法做到,当您拥有大量数据时,它们的性能都像废话一样。 OK if you lazy-load the related collections and only use a few, but if you need to display field from the child objects then you can get 100,000+ queries for a page load. 如果您懒加载相关集合并且仅使用几个,则可以,但是如果需要显示子对象的字段,则可以为页面加载获得100,000多个查询。 Believe me I just ahd to optimise a page using Doctrine that ran 200,000 queries. 相信我,我只是使用Doctrine优化了运行200,000个查询的页面。

What you have to do is either to paginate the results, or to write a custom query that joins the data and then process the results with a loop control: eg 您要做的是分页结果,或者编写一个自定义查询以连接数据,然后使用循环控件处理结果:例如

$sql = "SELECT * FROM table1 t1 JOIN table2 t2 ON t1.id = t2.fk_id";
// run query and get resdults
$cur_prod = '';
While( // fetch a row ){
  if($row['prod'] != $cur_prod){
    $cur_prod = $row['prod'];
    // start the new record in your layout
  }
  echo $row['t1.f1'];
  echo // blah blah blah the related data
}

What I am getting at here is that you have to track the parent product and manage the output when it changes. 我要说的是,您必须跟踪父产品并在产品更改时管理其输出。

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

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