Grails / Groovy - 在Map中返回MySQL记录只保留最后一条记录

时间:2017-08-22 00:34:29

标签: mysql grails groovy

我正在尝试遍历表数据并将表记录保存到Groovy Map中,但只有最后一条记录保存在地图中。

服务代码

#:kivy 1.10.0

<CustButton@Button>:
    font_size: 32
    color: 0, 0, 0, 1
    size: 150, 50
    background_normal: ''
    background_down: 'bt-down.png'
    background_color: .88, .88, .88, 1

<CustomWidget>:
    font_size: 32
    color: 0, 0, 0, 1
    size: 150, 150
    background_color: 0.88, 0.88, 0.88, 1
    CustButton:
        text: "Random"
        pos: root.x, 200
    CustButton:
        text: "Buttom"
        pos: 200, root.y
    CustButton:
        text: "Buttom"
        pos: 200, 400

以下员工的表记录:

enter image description here

这是完成迭代后Map的含义:

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个

    def list = []
    int count = 0
    sql.eachRow("SELECT * FROM employee") { row ->
        def tableMap = [:]
        tableMap.'first_name' = row.first_name
        tableMap.'last_name' = row.last_name
        tableMap.'born' = row.born
        list << tableMap
        print "\nIteration No " + count
        count++
    }
    list.each {
    for (def e in it) {
        print "key = ${e.key}, value = ${e.value}"
    }
}

您需要创建一个列表来存储所有地图,否则地图中的旧值将被新值替换。