简体   繁体   English

ibatis LazyQueryContainer-如何为SQL创建QueryDefinition?

[英]ibatis LazyQueryContainer - how to create QueryDefinition for a SQL?

I have a set of classes that can generate SQL code for me. 我有一组可以为我生成SQL代码的类。 I would like to use a generated SQL to implement the Query interface found in ibatis. 我想使用生成的SQL来实现ibatis中的Query接口。 I'm trying to create a special Query component that caches all results in memory. 我正在尝试创建一个特殊的查询组件,该组件将所有结果缓存在内存中。

I'm having trouble writing this method: 我在编写此方法时遇到了麻烦:

public void loadAllItems() {
    String sql = buildSQL();
    List<Map<String, Object>> query = DB.rows(sql, new RowBounds(0, maxRows+1));        
    this.rows = new ArrayList<Item>();
    for (Map<String,Object> row : query) {
        Item item = new PropertysetItem();
        // How can I create a QueryDefinition for my query???
        Collection<Object> propertyIds = definition.getPropertyIds();
        for (Object propertyId : propertyIds) {
            Object value = row.get(propertyId);
            if (value!=null) {
                item.addItemProperty(propertyId, new ObjectProperty(value));
            }
        }

        this.rows.add(item);
    }       
}

Here, the DB.rows method returns a List of rows. 在这里,DB.rows方法返回一个行列表。 For each row, column names are mapped to column values. 对于每一行,列名都映射到列值。 I have the rows and values for all columns. 我有所有列的行和值。 The only problem is that I cannot put results into a list of Item instances. 唯一的问题是我无法将结果放入Item实例列表中。 I don't know how to create a QueryDefinition instance for an arbitrary SQL. 我不知道如何为任意SQL创建QueryDefinition实例。

I know that there is an SQL builder ( http://mybatis.github.io/mybatis-3/statement-builders.html ) but I DO NOT want to use it. 我知道有一个SQL构建器( http://mybatis.github.io/mybatis-3/statement-builders.html ),但我不想使用它。 I want to use a plain SQL that was already generated, and put the results into a list of Item instances. 我想使用已经生成的普通SQL,并将结果放入Item实例列表中。 How do I do that? 我怎么做? What kind of properties do I need to assign before I could use these items to implement the Query interface of ibatis? 在使用这些项目实现ibatis的Query接口之前,需要分配哪种属性?

Okay, this was silly. 好吧,这很愚蠢。 They are named "properties" but actually they are just column names. 它们被命名为“属性”,但实际上它们只是列名。 So if I have a plain SQL and I know the names of the colum names then I don't need a QueryDefinition at all. 因此,如果我有一个普通的SQL并且知道列名的名称,那么我根本不需要QueryDefinition。 Suppose we have a list of column names called "colNames" for a SQL query: 假设我们有一个名为“ colNames”的SQL查询列名称:

public void loadAllItems() {
    String sql = buildSQL();
    List<Map<String, Object>> query = DB.rows(sql, new RowBounds(0, maxRows+1));        
    this.rows = new ArrayList<Item>();
    for (Map<String,Object> row : query) {
        Item item = new PropertysetItem();
        for (Object colName : this.colNames) {
            Object value = row.get(colName);
            if (value!=null) {
                item.addItemProperty(colName, new ObjectProperty(value));
            }
        }

        this.rows.add(item);
    }       
}

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

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