关于Spring queryforlist

时间:2014-07-10 17:32:21

标签: spring spring-mvc

我使用queryforlist

获取一些数据
[{TABLEID=1, MENUNAME=coke, MENUPRICE=1, MENUAMOUNT=2}, 
 {TABLEID=1, MENUNAME=sprite, MENUPRICE=1, MENUAMOUNT=1}]

我想删除属性名称。

[{1, coke, 1, 2}, 
 {1, sprite, 1, 1}]

我试图这样做:

public List<String> getOrder(String tableId) {
        String SQL = "SELECT * FROM ORDERTB WHERE TABLEID ='"+tableId+"'";

        List<Map> rows = getJdbcTemplate().queryForList(SQL);
        List <List<String>>result =  new ArrayList<List<String>>();
        List<String> temp = new ArrayList<String>(); 

        for (Map row : rows) {
            String id = (String)row.get("TABLEID");
            String name =(String)row.get("MENUNAME");
            String price =(String)row.get("MENUPRICE");
            String amount =(String)row.get("MENUAMOUNT");

            temp.add(id);
            temp.add(name);
            temp.add(price);
            temp.add(amount);

            result.add(temp);
            temp.clear();  // at this line, result's value is also removed
                           // I think I have to find other way.


        }
        System.out.println(result);  // result : [[],[]]
        // without temp.clear(); result : 
        return null; 
        //(List<String>)getJdbcTemplate().queryForList(SQL);
    }

没有temp.clear();然后,结果:

[{1, coke, 1, 2,1, sprite, 1, 1}, 
     {1, coke, 1, 2,1, sprite, 1, 1}]

是否有另一种api或方法使结果不为空?

1 个答案:

答案 0 :(得分:0)

只需在List<String> temp = new ArrayList<String>();循环中移动for

for (Map row : rows) {
    String id = (String)row.get("TABLEID");
    String name =(String)row.get("MENUNAME");
    String price =(String)row.get("MENUPRICE");         
    String amount =(String)row.get("MENUAMOUNT");

    List<String> temp = new ArrayList<String>(); 
    temp.add(id);
    temp.add(name);
    temp.add(price);
    temp.add(amount);

    result.add(temp);
}